Spring Boot – Configure maxSwallowSize in embedded Tomcat

In Spring Boot, you can’t configure the embedded Tomcat maxSwallowSize via the common application properties, there is no option like server.tomcat.*.maxSwallowSize

Solution

To fix it, you need to declare a TomcatEmbeddedServletContainerFactory bean and configure the maxSwallowSize like this :


//...
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;

    private int maxUploadSizeInMb = 10 * 1024 * 1024; // 10 MB

    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {

        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();

        tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
		
            // connector other settings...

            // configure maxSwallowSize
            if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
                // -1 means unlimited, accept bytes
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
			
        });

        return tomcat;

    }

References

  1. Spring file upload and connection reset issue
  2. common application properties
  3. Spring Boot file upload error handling (Japanase)

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Alex
8 years ago

The configuration like above didn’t help me(I’m using spring boot 1.5.10)

After some hours of research i found that the solution was to set
application.properties:

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

and all uploads works OK without configurating Tomcat.

(before i’ve trying alot with
spring.servlet.multipart.max-file-size=
spring.servlet.multipart.max-request-size=
and found that them didn’t work i.e. didn’t limit the file size, that helps me to find the answer)

BR, Alex

Shubham Sharma
6 years ago

In Spring 2.0 it is simple – server.tomcat.max-http-post-size=2MB
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

Alex
7 years ago

In SpringBoot2 things changed:

https://stackoverflow.com/questions/47700115/tomcatembeddedservletcontainerfactory-is-missing-in-spring-boot-2

@Bean
public TomcatServletWebServerFactory containerFactory() {
return new TomcatServletWebServerFactory() {
protected void customizeConnector(Connector connector) {
int maxSize = 50000000;
super.customizeConnector(connector);
connector.setMaxPostSize(maxSize);
connector.setMaxSavePostSize(maxSize);
if (connector.getProtocolHandler() instanceof AbstractHttp11Protocol) {

((AbstractHttp11Protocol ) connector.getProtocolHandler()).setMaxSwallowSize(maxSize);
logger.info(“Set MaxSwallowSize “+ maxSize);
}
}
};

}

Cwt
8 years ago

spring-boot 2.0
# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes “MB” or “KB” to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
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.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

xersiee
8 years ago

Tried this with no success. I’ve checked and setMaxSwallowSize(-1) is executed but I still get connection reset on large files.

Faraz
9 years ago

I created a new class and but the above code in there annotating the class as @Configuration, but nothing helps sadly. I am new to using Spring and don’t know what to do. Hope you can help.