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
- <bean id= "Multipartresolver" < span class= "keyword" >class=
- <property name= "MaxUploadSize" value= "104857600"/>&NBSP;&NBSP;
- <property name= "Maxinmemorysize" value= "4096"/>&NBSP;&NBSP;
- </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
- @RequestMapping (value="/multiplesave", Method=requestmethod.post)
- public @ResponseBody String multiplesave (@RequestParam ("file") multipartfile[] files) {
- String fileName = null;
- String msg = "";
- if (Files! = null && files.length >0) {
- For (int i =0;i< files.length; i++) {
- try {
- FileName = Files[i].getoriginalfilename ();
- byte[] bytes = Files[i].getbytes ();
- Bufferedoutputstream Buffstream =
- New Bufferedoutputstream (new FileOutputStream (NewFile ("/tmp/" + fileName));
- Buffstream.write (bytes);
- Buffstream.close ();
- msg + = "You have successfully uploaded" + FileName ";
- } catch (Exception e) {
- return "You failed to upload" + FileName + ":" + e.getmessage ();
- }
- }
- return msg;
- } Else {
- return "unable to upload. File is empty. ";
- }
- }
- }
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