4

My application server is Tomcat and I need is to upload files (large size) to my application. The body size of POST requests is really long like 15 MB or more.

Is there any type of configuration or code I can set to overcome this problem?

Please keep in mind that this is multipart request with file upload

Many thanks in advance

Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52
Barz8
  • 43
  • 1
  • 4

2 Answers2

4

Tomcat 7 and and above has a configuration called maxSwallowSize that specify number of bytes for an upload with a default value of 2MB. If your application is configured to accept bigger file, for example with a MultipartResolver in Spring, Tomcat will reject the request.

Because you can configure file size in your application, my advice is to disable maxShallowSize in {TOMCAT_HOME}/conf/server.xml using value -1 and then work only with size configured in your application

as follows:

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

You can find a detailed description here (with spring framerwork)

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
ValerioMC
  • 2,926
  • 13
  • 24
  • `maxSwallowSize` doesn't seem to be the max upload size. _The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload._ – Gustavo Jun 26 '20 at 01:35
1

1. First configure max file upload in Spring ( override default value 1MB)

form spring mvc create bean :

@Bean
public CommonsMultipartResolver multipartResolver() {
     CommonsMultipartResolver resolver=new CommonsMultipartResolver();
     resolver.setMaxUploadSize(15728640);
     resolver.setMaxUploadSizePerFile(15728640);
     return resolver;
}

for spring boot : add in application.properties

spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB

2. Second thing configure your tomcat :

locate your server xml tomcat file $TOMCAT_HOME/conf/server.xml

then chnage your connector as below by example :

<Connector port="8080" protocol="HTTP/1.1"
  connectionTimeout="20000"
  redirectPort="8443"
  maxSwallowSize = "-1"/>
Bourbia Brahim
  • 14,459
  • 4
  • 39
  • 52