Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBoot's @MultipartConfig maxFileSize not taking effect

I have a controller with a MultipartConfig annotation (a snippet of which is show below):

@RestController @RequestMapping("packages") @MultipartConfig(maxFileSize = 1024*1024*1024, maxRequestSize = 1024*1024*1024) public class PackagesController {     @RequestMapping(method = RequestMethod.POST)     public String create(@RequestParam("package") MultipartFile uploadedPackage)     {         // do stuff to the file         return "create";     } } 

When I upload a file to this endpoint, though, it appears to be using the default multipart config values:

Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field package exceeds its maximum permitted size of 1048576 bytes.     at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:633) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_45]     at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     at org.apache.catalina.connector.Request.parseParts(Request.java:2776) ~[tomcat-embed-core-8.0.32.jar:8.0.32]     ... 30 common frames omitted 

My application looks like this:

@SpringBootApplication public class Application {      public static void main(String[] args) {         ApplicationContext ctx = SpringApplication.run(Application.class, args);     }  } 

Is there something I'm missing that allows the @MultipartConfig annotation to take effect?

like image 454
Nic Wolfe Avatar asked Apr 29 '16 18:04

Nic Wolfe


2 Answers

With Spring Boot 2.0, you should use this in your application.yml

spring:   servlet:     multipart:       max-file-size: 100MB       max-request-size: 100MB 

From documentation:

Spring Boot embraces the Servlet 3 javax.servlet.http.Part API to support uploading files. By default, Spring Boot configures Spring MVC with a maximum size of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, the location to which intermediate data is stored (for example, to the /tmp directory), and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. For example, if you want to specify that files be unlimited, set the spring.servlet.multipart.max-file-size property to -1.

Extracted from Appendix A of documentation

spring.servlet.multipart.max-file-size=1MB # Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.

spring.servlet.multipart.max-request-size=10MB # Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.

like image 171
T4keo Avatar answered Sep 24 '22 23:09

T4keo


If you just want to control the multipart properties, you can use multipart.max-file-size and multipart.max-request-size properties. For example, you could raise the max size to 100Mb by adding following piece of configurations in your application.properties file:

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

Values can use the suffixed MB or KB to indicate a Megabyte or Kilobyte size.

Under the hood, Spring Boot will create a MultipartConfigElement based on MultipartProperties and that MultipartConfigElement will be used in Servlet registration, as stated in Spring MVC documentation. You can take a look at MultipartAutoConfiguration and DispatcherServletConfiguration and Checkout Spring Boot documentation for more information.

like image 40
Ali Dehghani Avatar answered Sep 24 '22 23:09

Ali Dehghani