Multipart file maximum size configuration in Tomca

2020-06-25 05:01发布

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

2条回答
可以哭但决不认输i
2楼-- · 2020-06-25 05:20

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"/>
查看更多
干净又极端
3楼-- · 2020-06-25 05:24

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)

查看更多
登录 后发表回答