Main Tutorials

Spring file upload and connection reset issue

A Spring servlet initializer to configure the file upload limit, 5mb per file and 10mb per request.

MyWebInitializer.java

public class MyWebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

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

    //...

    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {

        // upload temp file will put here
        File uploadDirectory = new File(System.getProperty("java.io.tmpdir"));

        // register a MultipartConfigElement
        MultipartConfigElement multipartConfigElement =
                new MultipartConfigElement(uploadDirectory.getAbsolutePath(),
                        maxUploadSizeInMb, maxUploadSizeInMb * 2, maxUploadSizeInMb / 2);

        registration.setMultipartConfig(multipartConfigElement);

    }

}

If you try to upload a file size bigger than the upload limit, let says 20mb, the following weird “connection resets” error will be displayed:

spring-file-upload-connection-reset

Even the global @ControllerAdvice can’t catch the above exception!

Tested

  1. Spring 4.3.5.RELEASE
  2. Tomcat 8
  3. Spring MVC file upload example

1. Solution

After a few days of investigation and research, I find out the problem was not related to Spring (no problem if deployed to Jetty), it’s the Tomcat default connector maxSwallowSize

Quote – Apache Tomcat 8 Configuration Reference

The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored, but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2097152 (2 megabytes) will be used. A value of less than zero indicates that no limit should be enforced.

If you try to upload a file size, which exceeded the size limit, Tomcat will crop it at the default 2mb and reset the connection. To solve it quickly, put a -1 (unlimited) for maxSwallowSize

Tomcat/conf/server.xml

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxSwallowSize="-1" />

This -1 is good for development, but not for production, if user tries to upload a 100mb file, Tomcat will waste resources to handle the extra bandwidth.

2. Idea

2. 1 Try to put a reasonable limit for the file upload, for example, 11mb (11 x 1024 x 1024 = bytes) in this example

Tomcat/conf/server.xml

    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxSwallowSize="11534336" />

2.2 And implement a Javascript to check the file size before the upload.

What about Jetty?
Tested with Jetty 9, can’t find out any maxSwallowSize setting, and it will NOT cause a connection reset if the file size has exceeded the upload limit.

References

  1. Apache Tomcat 8 Configuration Reference
  2. Setting a tomcat property using spring boot application properties
  3. Spring MVC file upload example

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
sobev
1 year ago

thanks, it’s working finally🤯

Herbert
9 months ago

You are the best!!

Oleg Tomi
4 years ago

It was real helpful to me. Thank you.

Mahrosh
4 years ago

Hi Mkyong, ur posts are very conducive to me. I am in same issue nowadays but i doubt that the server side may be websphere or Glassfish or tomcat. Although i m receiving same error “Error writing request body to server”…. Can You Kindly tell me whats counterpart property of Tomcat:MaxSwallowSize in Websphere and in Glassfish respectively????

xtian
6 years ago

Really good solution to use both javascript and server check

josuntae
6 years ago

Hello I am Cho Soon-tae, who has read your website in Korea and solved the same problem.
Thank you for leaving a good example. If you have any ideas other than 2 ideas, I would appreciate your link.
I have processed it as 1 solution first.
Thank you.

Durim Kryeziu
6 years ago

Hi,

What do you suggest if I want to allow far more file size upload in production ?

Thanks for this article

Kaikou Li
7 years ago

very good! it really helps!