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

Wim Deblauwe picture Wim Deblauwe · Sep 5, 2014 · Viewed 7.9k times · Source

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.

Answer

Ondrej Bozek picture Ondrej Bozek · Apr 23, 2015

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