How to upload multiple files at once using MultipartConfigElement in Spring Download?

I am using Spring Boot 1.1.3 with CommonsMultipartResolver to allow multiple files to download at the same time.

I get this stack when I try to upload a file larger than 1 MB:

 Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field files[] exceeds its maximum permitted size of 1048576 bytes. at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:637) at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) at java.io.FilterInputStream.read(FilterInputStream.java:107) 

I tried to set the maximum download size as follows:

 public MultipartResolver multipartResolver() { CommonsMultipartResolver resolver = new CommonsMultipartResolver(); resolver.setMaxUploadSize( 100 * MEGABYTE_IN_BYTES ); return resolver; } 

However, this does not work. I found this Spring file upload guide and they use MultipartConfigFactory instead. However, now I need to use the MultipartFile class instead of the MultipartHttpServletRequest in my controller.

Using MultipartHttpServletRequest I could do getFileMap() to get all the files, but there is no such method on MultipartFile .

Any ideas on how to work with MultipartConfigFactory and multiple files? I use jquery-file-upload on the client, if that matters.

+5
source share
4 answers

It seems there is no need to use MultipartFile in the controller, you can also use MultipartHttpServletRequest . This is the signature of my annotated @RestController class:

 @RequestMapping(value = "/{datasheetId}/addDoc", method = RequestMethod.POST) @Secured(ROLE_USER) public ResponseEntity<?> addDocumentToDatasheet( Principal principal, @PathVariable("datasheetId") long datasheetId, MultipartHttpServletRequest request ) throws IOException { Map<String, MultipartFile> fileMap = request.getFileMap(); // handle fileMap further... } 
+1
source

The correct way to increase the load limit is to set the property
multipart.maxFileSize=10Mb
In your application.properties file. You can read more on this topic here fooobar.com/questions/451638 / ... and here MultipartProperties.java

+7
source

A question was asked regarding Spring Boot 1.1.3. Over time, the decisions taken no longer work with newer versions of Spring Boot. However, the problem of increasing the maximum allowable download size remains.

I am writing this as from Spring Boot 1.4.2, so this solution may be wrong forever.

From Spring's guide to downloading files, new properties:

 spring.http.multipart.max-file-size=10Mb spring.http.multipart.max-request-size=10Mb 
+3
source

for the size problem in my project I use these properties in application.properties:

 multipart.max-file-size=100Mb multipart.max-request-size=100Mb 

for the case with multiple files, I suggest using a bean like this

 @Data class MultipartDTO{ private MultipartFile file1; private MultipartFile file2; ..... private MultipartFile filen; //eventually other proerties } 

then of course your controller you have something like this

 @RequestMapping(value = "/multipartUrl", method = RequestMethod.POST, consumes = "multipart/form-data") public ResponseEntity multipartCall(MultipartDTO multipartDTO){ .... } 

Hope this helps you.

+1
source

Source: https://habr.com/ru/post/1201873/


All Articles