java - working - tomcat the field file exceeds its maximum permitted size of




SpringBoot's @MultipartConfig maxFileSize not taking effect (4)

For setting custom limits for multipart uploads, the below properties are to be used(for a sample size of 30MB):

spring.http.multipart.max-file-size=30MB
spring.http.multipart.max-request-size=30MB

In our company's projects, I found that one of them was on Spring Boot version 1.3.5, so for versions < 1.4 you should use

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

From the docs(v1.4.0):

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 file of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the /tmp directory) and the threshold past which data is flushed to disk by using the properties exposed in the MultipartProperties class. If you want to specify that files be unlimited, for example, set the spring.http.multipart.max-file-size property to -1.

The multipart support is helpful when you want to receive multipart encoded file data as a @RequestParam-annotated parameter of type MultipartFile in a Spring MVC controller handler method.

Same docs for version 1.3.8:

If you want to specify that files be unlimited, for example, set the multipart.maxFileSize property to -1.

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?


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.


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.


With spring-boot 1.5.3 you should use the following code in application.yml

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

Make sure to use spaces and not tab in your yaml file.





spring-boot