利用MultipartFile实现文件上传


利用MultipartFile实现文件上传

         在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的MultipartFile实现文件上传,感觉挺简单,在这里和大家分享一下.

一.主要有两个java类,和一般的servlet放在一起即可.

1.FileUploadBean.java

package chb.demo.web;
    


import org.springframework.web.multipart.MultipartFile;
    


/**

 * @author chb

 *

 */
    

public 
    class FileUploadBean 
    {


    private MultipartFile file;


    public void setFile(MultipartFile file) {

        this.file = file;

    }


    public MultipartFile getFile() {

        return file;

    }

}
    
2.FileUploadController.java
    
 


    
package chb.demo.web;
    


import java.io.FileOutputStream;
    

import java.io.IOException;
    

import java.io.InputStream;
    


import javax.servlet.http.HttpServletRequest;
    

import javax.servlet.http.HttpServletResponse;
    


import org.springframework.validation.BindException;
    

import org.springframework.web.multipart.MultipartFile;
    

import org.springframework.web.servlet.ModelAndView;
    

import org.springframework.web.servlet.mvc.SimpleFormController;
    



/**

 * @author chb

 *

 */
    

public 
    class FileUploadController 
    extends SimpleFormController 
    {

        

    protected ModelAndView onSubmit(

        HttpServletRequest request,

        HttpServletResponse response,

        Object command,

        BindException errors){

        

        try

        {

            // cast the bean

            FileUploadBean bean = (FileUploadBean) command;


            // let's see if there's content there

            MultipartFile file = bean.getFile();

                               

            if (file == null) {

                throw new Exception("上传失败:文件为�空");    

            }

            if(file.getSize()>10000000)        

            {

                throw new Exception("上传失败:文件大小不能超过10M");            

            }

            //得到文件�名

            String filename=file.getOriginalFilename();        

            

            if(file.getSize()>0){                

                try {

                    SaveFileFromInputStream(file.getInputStream(),"D:/",filename);

                } catch (IOException e) {

                    System.out.println(e.getMessage());

                    return null;

                }

            }

            else{

                throw new Exception("上传失败:上传文件不能为�空");

            }

            // well, let's do nothing with the bean for now and return:

            try {

                return super.onSubmit(request, response, command, errors);

                

            } catch (Exception e) {

                System.out.println(e.getMessage());

                return null;

            }

        }

        catch(Exception ex)

        {

            System.out.println(ex.getMessage());

            return null;

        }

    }   

    

    /**保存文件

     * @param stream

     * @param path

     * @param filename

     * @throws IOException

     */

    public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException

    {      

        FileOutputStream fs=new FileOutputStream( path + "/"+ filename);

        byte[] buffer =new byte[1024*1024];

        int bytesum = 0;

        int byteread = 0; 

        while ((byteread=stream.read(buffer))!=-1)

        {

           bytesum+=byteread;

           fs.write(buffer,0,byteread);

           fs.flush();

        } 

        fs.close();

        stream.close();      

    }       

}


利用MultipartFile实现文件上传

         在java中上传文件似乎总有点麻烦,没.net那么简单,记得最开始的时候用smartUpload实现文件上传,最近在工作中使用spring的MultipartFile实现文件上传,感觉挺简单,在这里和大家分享一下.

一.主要有两个java类,和一般的servlet放在一起即可.

1.FileUploadBean.java

package chb.demo.web;
    
import org.springframework.web.multipart.MultipartFile;
    
/**
 * @author chb
 *
 */
    
public 
    class FileUploadBean 
    {
    private MultipartFile file;
    public void setFile(MultipartFile file) {
        this.file = file;
    }
    public MultipartFile getFile() {
        return file;
    }
}


2.FileUploadController.java

package chb.demo.web;
    


import java.io.FileOutputStream;
    

import java.io.IOException;
    

import java.io.InputStream;
    


import javax.servlet.http.HttpServletRequest;
    

import javax.servlet.http.HttpServletResponse;
    


import org.springframework.validation.BindException;
    

import org.springframework.web.multipart.MultipartFile;
    

import org.springframework.web.servlet.ModelAndView;
    

import org.springframework.web.servlet.mvc.SimpleFormController;
    



/**

 * @author chb

 *

 */
    

public 
    class FileUploadController 
    extends SimpleFormController 
    {

        

    protected ModelAndView onSubmit(

        HttpServletRequest request,

        HttpServletResponse response,

        Object command,

        BindException errors){

        

        try

        {

            // cast the bean

            FileUploadBean bean = (FileUploadBean) command;


            // let's see if there's content there

            MultipartFile file = bean.getFile();

                               

            if (file == null) {

                throw new Exception("上传失败:文件为�空");    

            }

            if(file.getSize()>10000000)        

            {

                throw new Exception("上传失败:文件大小不能超过10M");            

            }

            //得到文件�名

            String filename=file.getOriginalFilename();        

            

            if(file.getSize()>0){                

                try {

                    SaveFileFromInputStream(file.getInputStream(),"D:/",filename);

                } catch (IOException e) {

                    System.out.println(e.getMessage());

                    return null;

                }

            }

            else{

                throw new Exception("上传失败:上传文件不能为�空");

            }

            // well, let's do nothing with the bean for now and return:

            try {

                return super.onSubmit(request, response, command, errors);

                

            } catch (Exception e) {

                System.out.println(e.getMessage());

                return null;

            }

        }

        catch(Exception ex)

        {

            System.out.println(ex.getMessage());

            return null;

        }

    }   

    

    /**保存文件

     * @param stream

     * @param path

     * @param filename

     * @throws IOException

     */

    public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException

    {      

        FileOutputStream fs=new FileOutputStream( path + "/"+ filename);

        byte[] buffer =new byte[1024*1024];

        int bytesum = 0;

        int byteread = 0; 

        while ((byteread=stream.read(buffer))!=-1)

        {

           bytesum+=byteread;

           fs.write(buffer,0,byteread);

           fs.flush();

        } 

        fs.close();

        stream.close();      

    }       

}