Application of Multipartfile multi-file upload

Source: Internet
Author: User
Tags jquery file upload

The company's projects in many places to use the file upload, the previous upload is mainly using Apache FileUpload, the use of the feeling is not very good. Try spring Multipartfile today, feel good, the package is relatively concise.

Of course, the middle also ruled out a lot of pits.

1. Configure Commonsmultipartresolver

1     <!--Configure Multipartresolver for file uploads using spring's commosmultipartresolver -2     <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">3         < Propertyname= "Defaultencoding"value= "UTF-8"/>4         < Propertyname= "Maxuploadsize"value= "209715200"/>5         < Propertyname= "Maxinmemorysize"value= "4096"/>6         < Propertyname= "Uploadtempdir"value= "Fileupload/temp"/>7     </Bean>

Defaultencoding= "UTF-8" is the requested encoding format, the default is Iso-8859-1
Maxuploadsize= "209715200" is the total size of the uploaded file, in bytes, I set the 200M,
Uploadtempdir= "Fileupload/temp" is the temporary path for uploading files

2. Add Apache's FileUpload

1 <Dependency>2     <groupId>Commons-fileupload</groupId>3     <Artifactid>Commons-fileupload</Artifactid>4     <version>1.2.2</version>5 </Dependency>

Should be the multipartfile way to upload the bottom will also use Commons-fileupload

3. The front end uses the jquery file upload

1 <spanclass= "btn btn-success Fileinput-button">2     <Iclass= "Glyphicon glyphicon-plus"></I>3     <span>Select File ...</span>4     <!--The file input field used as target for the file upload widget -5     <inputID= "FileUpload"type= "File"name= "file[]"multiple>6 </span>7#jquery file upload is not uploaded using form form, it is the Ajax way to send the request to the background,
#如果是以表单形式提交上传请求, don't forget to addenctype="Multipart/form-data"
8 ...9 Ten $ (' #fileupload '). FileUpload ({ OneURL: "<%=Request.getcontextpath ()%>/aaa/upload.do ", A Sequentialuploads:true, - Singlefileuploads:false, - add:function (E, data) { theData.context = $ ('<Button/>'). Text (' Upload ') - . AppendTo (document.body) - . Click (function () { -Data.context = $ ('<P/>'). Text (' uploading ... '). ReplaceAll ($ (this)); + data.submit (); - }); + }, A done:function (E, data) { at $.each (data.files, function (index, file) { -$ ('<P/>'). Text (file.name). AppendTo (document.body); - }); - } -});

jquery file upload looks good, is the Chinese information or a little bit, the official website look at the head big.

4. Backstage is our focus, in fact, I would like to say that I just want to find a good weekend, beautiful front-end upload plug-in, the results focus on the backstage to come.

File upload request HttpServletRequest will be resolved to Multiparthttpservletrequest because Commonsmultipartresolver is configured

1  /**2 * Multi-File Upload3      *4      * @parammultipartrequest5      * @return6      * @throwsIOException7      */8 @ResponseBody9@RequestMapping (value = "Upload", method =requestmethod.post)Ten      PublicString Handleimport (defaultmultiparthttpservletrequest multipartrequest)throwsIOException { One         if(Multipartrequest! =NULL) { AIterator<string> Iterator =multipartrequest.getfilenames (); -              while(Iterator.hasnext ()) { -  the //                //Single File Upload.  - //multipartfile file = Multipartrequest.getfile (Iterator.next ());//pass one file at a time - //if (Stringutils.hastext (File.getoriginalfilename ())) { - //File.transferto (New file ("E:/upload_" + File.getoriginalfilename ())); + //                } -  +                 //multiple file uploads Alist<multipartfile> fileList = Multipartrequest.getfiles (Iterator.next ());//Select multiple file uploads at a time at                  for(Multipartfile file:filelist) { -                     if(Stringutils.hastext (File.getoriginalfilename ())) { -File.transferto (NewFile ("E:/upload_" +file.getoriginalfilename ())); -                     } -                 } -             } in         } -         return"Success"; to}

Application of Multipartfile multi-file upload

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.