Spring-boot uploading a file multipartfile getting no file Problem resolution

Source: Internet
Author: User

1. The phenomenon is that the commons-fileupload jar is added to the spring-boot and the Mutilpart Bean is configured, and after the upload POST request, the

Multipartrequest.getfiles ("file") =null, a bit strange, check the documentation to solve.
[Java]View PlainCopy
    1. <bean id= "Multipartresolver"  < span class= "keyword" >class=
    2.        <property name= "MaxUploadSize"  value= "104857600"/>&NBSP;&NBSP;
    3.         <property name= "Maxinmemorysize"  value= "4096"/>&NBSP;&NBSP;
    4.    </bean>  

2. The reason is: Spring-boot comes with Org.springframework.web.multipart.MultipartFile
Conflicts with multipart, and if both Multipartresolver and Servletfileupload are used, false is returned in Iter.hasnext (). Then the whole loop jumps out. The whole problem arises because the spring framework calls Multipartresolver to handle HTTP multi-part requests first. Here the HTTP multipart request has been consumed. Back to Servletfileupload, then Servletfileupload will not get the corresponding multi-part request. As a result, the Multipartresolve configuration is removed and the problem is resolved.

3. Single file only need a variable that is, multi-file upload the words will be multipartfile to the array, and then upload and save separately.

[Java]View PlainCopy
  1. @RequestMapping (value="/multiplesave", Method=requestmethod.post)
  2. public @ResponseBody String multiplesave (@RequestParam ("file") multipartfile[] files) {
  3. String fileName = null;
  4. String msg = "";
  5. if (Files! = null && files.length >0) {
  6. For (int i =0;i< files.length; i++) {
  7. try {
  8. FileName = Files[i].getoriginalfilename ();
  9. byte[] bytes = Files[i].getbytes ();
  10. Bufferedoutputstream Buffstream =
  11. New Bufferedoutputstream (new FileOutputStream (NewFile ("/tmp/" + fileName));
  12. Buffstream.write (bytes);
  13. Buffstream.close ();
  14. msg + = "You have successfully uploaded" + FileName ";
  15. } catch (Exception e) {
  16. return "You failed to upload" + FileName + ":" + e.getmessage ();
  17. }
  18. }
  19. return msg;
  20. } Else {
  21. return "unable to upload. File is empty. ";
  22. }
  23. }
  24. }

4.spring-boot Configure the maximum limit for uploading files and requesting files:
Directly in the application.properties.
multipart.maxfilesize=128kb
multipart.maxrequestsize=128kb

5. Is spring-boot-starter-web already added as dependencies. To upload files with the Servlet containers, you need to register a MultipartConfigElement class (which would is in <multipart-config> Web. xml). Thanks to Spring Boot, everything are auto-configured for you! Spring-boot-upload Links

Spring-boot uploading a file multipartfile getting no file Problem resolution

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.