Increase HTTP Post maxPostSize in Spring Boot

2020-01-25 05:51发布

I've got a fairly simple Spring Boot web application, I have a single HTML page with a form with enctype="multipart/form-data". I'm getting this error:

The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.

I'm using Spring Boot's default embedded tomcat server. Apparently the default maxPostSize value is 2 megabytes. Is there any way to edit this value? Doing so via application.properties would be best, rather than having to create customized beans or mess with xml files.

10条回答
来,给爷笑一个
2楼-- · 2020-01-25 06:36

First, make sure you are using spring.servlet instead of spring.http.

---
spring:
  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 10MB

If you have to use tomcat, you might end up creating EmbeddedServletContainerCustomizer, which is not really nice thing to do.

If you can live without tomat, you could replace tomcat with e.g. undertow and avoid this issue at all.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-01-25 06:39

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

查看更多
Anthone
4楼-- · 2020-01-25 06:39

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.

查看更多
放荡不羁爱自由
5楼-- · 2020-01-25 06:51

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.
查看更多
狗以群分
6楼-- · 2020-01-25 06:51

For me nothing of previous works (maybe use application with yaml is an issue here), but get ride of that issue using that:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

@ServletComponentScan
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(DataSize.ofBytes(512000000L));
        factory.setMaxRequestSize(DataSize.ofBytes(512000000L));
        return factory.createMultipartConfig();
    }
}
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-01-25 06:52

There is some difference when we define the properties in the application.yaml and application.properties.

In application.yml:

spring:
  http:
    multipart:
      max-file-size: 256KB
      max-request-size: 256KB

And in application.propeties:

spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

Note: Spring version 4.3 and Spring boot 1.4

查看更多
登录 后发表回答