File Upload Stream Processing commons-fileupload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.