1. Get Multipartfile from the request
@RequestMapping (value= "/upload", method=requestmethod.post) public @ResponseBody String Handlefileupload ( @RequestParam ("file") multipartfile file throws ioexception{
2. File Stream Processing
ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportOrg.apache.commons.fileupload.util.Streams;Importorg.apache.commons.io.IOUtils;ImportOrg.springframework.web.multipart.MultipartFile; Public classUploadfileutil {/*** Use the same name *@paramfile *@paramFilePath *@return */ Public StaticString handlefileupload (multipartfile file,string filePath) {Try { if(!File.isempty ()) {String name=File.getoriginalfilename (); InputStream Inpustream=File.getinputstream (); FileOutputStream Fos=NewFileOutputStream (filepath+file.separator+name); Streams.copy (Inpustream, FOS,true); }Else{ return"Can not upload the file,because it is empty!"; } } Catch(IOException e) {return"Failed to upload this file,because Stream interrupt unexpectedly"; } return"Upload file sucess"; } /*** Rename file with suffix *@paramfile *@paramFilePath *@paramFileName *@return */ Public StaticString handlefileupload (multipartfile file,string filepath,string fileName) {Try { if(!File.isempty ()) {InputStream Inpustream=File.getinputstream (); FileOutputStream Fos=NewFileOutputStream (filepath+file.separator+fileName); Streams.copy (Inpustream, FOS,true); }Else{ return"Can not upload the file,because it is empty!"; } } Catch(IOException e) {return"Failed to upload this file,because Stream interrupt unexpectedly"; } return"Upload file sucess"; } /***copy bytes from a large (over 2GB), file with same filename *@paramfile *@paramFilePath *@paramFileName *@return */ Public StaticString handlelargefileupload (multipartfile file,string filePath) {Try { if(!File.isempty ()) {String name=File.getoriginalfilename (); InputStream Inpustream=File.getinputstream (); FileOutputStream Fos=NewFileOutputStream (filepath+file.separator+name); Ioutils.copylarge (Inpustream, FOS); if(inpustream!=NULL) {inpustream.close (); } if(fos!=NULL) {fos.close (); } }Else{ return"Can not upload the file,because it is empty!"; } } Catch(IOException e) {return"Failed to upload this file,because Stream interrupt unexpectedly"; } return"Upload file sucess"; } /***copy bytes from a large (over 2GB), file with rename file *@paramfile *@paramFilePath *@paramFileName *@return */ Public StaticString handlelargefileupload (multipartfile file,string filepath,string fileName) {Try { if(!File.isempty ()) {InputStream Inpustream=File.getinputstream (); FileOutputStream Fos=NewFileOutputStream (filepath+file.separator+fileName); Ioutils.copylarge (Inpustream, FOS); if(inpustream!=NULL) {inpustream.close (); } if(fos!=NULL) {fos.close (); } }Else{ return"Can not upload the file,because it is empty!"; } } Catch(IOException e) {return"Failed to upload this file,because Stream interrupt unexpectedly"; } return"Upload file sucess"; }}
File Upload Stream Processing commons-fileupload