Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Spring Boot 1.1.3 with the CommonsMultipartResolver to allow uploading of multiple files at once.

I get this stacktrace when I try to upload a file bigger 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 setting the max upload size like this:

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

However, this does not work. I have found this Spring guide on upoading files and there they use MultipartConfigFactory instead. However, I now need to use the MultipartFile class instead of MultipartHttpServletRequest in my controller.

With the 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 am using jquery-file-upload on the client if that would matter.

like image 587
Wim Deblauwe Avatar asked Sep 05 '14 08:09

Wim Deblauwe


4 Answers

The right way to increase upload limit is to set property
multipart.maxFileSize=10Mb
In your application.properties file. You can read more on this topic here https://stackoverflow.com/a/27062232/668417 and here MultipartProperties.java

like image 55
Ondrej Bozek Avatar answered Sep 27 '22 20:09

Ondrej Bozek


The question was asked with regard to Spring Boot 1.1.3. As time marches forward the accepted solutions no longer work with newer versions of Spring Boot. However the problem of increasing the maximum allowable upload size persists.

I am writing this as of Spring Boot 1.4.2, so this solution may not be correct in perpetuity.

From the spring guide to uploading files the new properties are thus:

spring.http.multipart.max-file-size=10Mb
spring.http.multipart.max-request-size=10Mb
like image 24
abh Avatar answered Sep 27 '22 19:09

abh


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

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

for the case of have multiple file I suggest of use a bean like this

@Data
class MultipartDTO{

    private MultipartFile file1; 
    private MultipartFile file2;
    .....
    private MultipartFile filen; 

    //eventually other proerties
}

then of course in your controller you colud have a thing like this

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

I hope this can help you

like image 42
Valerio Vaudi Avatar answered Sep 27 '22 19:09

Valerio Vaudi


Seems there is no need to use the MultipartFile in the Controller, you can also use the MultipartHttpServletRequest. This is the signature of my @RestController annotated 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...
}
like image 31
Wim Deblauwe Avatar answered Sep 27 '22 21:09

Wim Deblauwe