Increase HTTP Post maxPostSize in Spring Boot

Apply settings for Tomcat as well as servlet

You can set the max post size for Tomcat in application.properties which is set with an int as below. Just setting spring.servlet.multipart.max-request-size=10MB, as in some other answers, may not be enough.

# Web properties
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

# Server properties
server.tomcat.max-http-post-size=100000000
server.tomcat.max-swallow-size=100000000

Working with Spring Boot 2.0.5.RELEASE


Found a solution. Add this code to the same class running SpringApplication.run.

// Set maxPostSize of embedded tomcat server to 10 megabytes (default is 2 MB, not large enough to support file uploads > 1.5 MB)
@Bean
EmbeddedServletContainerCustomizer containerCustomizer() throws Exception {
    return (ConfigurableEmbeddedServletContainer container) -> {
        if (container instanceof TomcatEmbeddedServletContainerFactory) {
            TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) container;
            tomcat.addConnectorCustomizers(
                (connector) -> {
                    connector.setMaxPostSize(10000000); // 10 MB
                }
            );
        }
    };
}

Edit: Apparently adding this to your application.properties file will also increase the maxPostSize, but I haven't tried it myself so I can't confirm.

multipart.maxFileSize=10Mb # Max file size.
multipart.maxRequestSize=10Mb # Max request size.

If you are using using x-www-form-urlencoded mediatype in your POST requests (as I do), the multipart property of spring-boot does not work. If your spring-boot application is also starting a tomcat, you need to set the following property in your application.properties file:

# Setting max size of post requests to 6MB (default: 2MB)
server.tomcat.max-http-post-size=6291456

I could not find that information anywhere in the spring-boot documentations. Hope it helps anybody who also sticks with x-www-form-urlencoded encoding of the body.


In application.properties file write this:

# Max file size.
spring.http.multipart.max-file-size=1Mb
# Max request size.
spring.http.multipart.max-request-size=10Mb

Adjust size according to your need.


Update

Note: As of Spring Boot 2, however you can now do

# Max file size.
spring.servlet.multipart.max-file-size=1MB
# Max request size.
spring.servlet.multipart.max-request-size=10MB

Appendix A. Common application properties - Spring