Increase HTTP Post maxPostSize in Spring Boot

JavaSpringSpring MvcTomcat

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

Apply settings for Tomcat as well as servlet

You can set the max post size for Tomcat in application.properties which is set with an int as below. Just setting spring.servlet.multipart.max-request-size=10MB, as in some other answers, may not be enough.

# Web properties
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

# Server properties
server.tomcat.max-http-post-size=100000000
server.tomcat.max-swallow-size=100000000

Working with Spring Boot 2.0.5.RELEASE

Solution 5 - Java

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.

Solution 6 - Java

The error here is not caused by max-file-size or max-request-size (as pointed out) but rather the container-specific max-http-post-size property. For tomcat (the default container), you can set:

server.tomcat.max-http-post-size: 10MB

Jetty:

server.jetty.max-http-post-size: 10MB

Undertow:

server.undertow.max-http-post-size: 10MB

This has the same effect as OP's answer here, but via application.properties which is much more preferable.

Solution 7 - Java

from documentation: https://spring.io/guides/gs/uploading-files/

> Tuning file upload limits > > When configuring file uploads, it is often useful to set limits on the > size of files. Imagine trying to handle a 5GB file upload! With Spring > Boot, we can tune its auto-configured MultipartConfigElement with some > property settings. > > Add the following properties to your existing > src/main/resources/application.properties: > > spring.http.multipart.max-file-size=128KB > > spring.http.multipart.max-request-size=128KB > > The multipart settings are constrained as follows: > > - spring.http.multipart.max-file-size is set to 128KB, meaning total > file size cannot exceed 128KB. > > - spring.http.multipart.max-request-size > is set to 128KB, meaning total request size for a multipart/form-data > cannot exceed 128KB.

Solution 8 - Java

None of the solutions did work for me and most of them are just downright offtopic because OP is talking about maxPostSize, and not maxFileSize (latter gives you a different error anyway if the size is exceeded)

Solution: in /tomcat/conf/server.xml add maxPostSize="" attribute to Connector

<!-- A "Connector" represents an endpoint by which requests are received
     and responses are returned. Documentation at :
     Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
     Java AJP  Connector: /docs/config/ajp.html
     APR (HTTP/AJP) Connector: /docs/apr.html
     Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
		   maxPostSize="10485760"
           redirectPort="8443" />

Solution 9 - Java

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

Solution 10 - Java

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();
    }
}

Solution 11 - Java

I do face kind a similar issue. where I was getting -

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

Solution I have applied following property, where using max-http-form-post-size property did make the difference for me -

server:
  tomcat:
    max-http-form-post-size: 100000000
    max-swallow-size: 100000000

Along with -

spring:
  servlet:
    multipart:
      maxFileSize: 10MB
      maxRequestSize: 10MB

And it worked for me.

Solution 12 - Java

In Spring Boot >2 version, you can simply add following to the "application.properties" file:

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=20MB

Solution 13 - Java

Spring-boot 2.2.x has changed

server.tomcat.max-http-post-size 

to

server.tomcat.max-http-form-post-size

spring-boot-issue

Solution 14 - Java

For me this worked in yaml

spring:
    profiles: ....
    application:
        name:"...."
    http:
        multipart:
            max-file-size: 2147483648
            max-request-size: 2147483648

Solution 15 - Java

This worked for me with Tomcat 8

MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Catalina:type=Connector,port=" + 8080);
mbeanServer.setAttribute(objectName, new Attribute("maxPostSize", 100000000));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordanView Question on Stackoverflow
Solution 1 - JavaSanjay Singh RawatView Answer on Stackoverflow
Solution 2 - JavaJordanView Answer on Stackoverflow
Solution 3 - JavaFabi KoView Answer on Stackoverflow
Solution 4 - JavaSteve BantonView Answer on Stackoverflow
Solution 5 - JavaOndrej KvasnovskyView Answer on Stackoverflow
Solution 6 - Java1615903View Answer on Stackoverflow
Solution 7 - JavaDániel KisView Answer on Stackoverflow
Solution 8 - JavaspiritworldView Answer on Stackoverflow
Solution 9 - JavadenzalView Answer on Stackoverflow
Solution 10 - Javauser11999314View Answer on Stackoverflow
Solution 11 - JavaLokesh GuruView Answer on Stackoverflow
Solution 12 - JavaDEV10View Answer on Stackoverflow
Solution 13 - JavamkboelView Answer on Stackoverflow
Solution 14 - JavaMaayan HopeView Answer on Stackoverflow
Solution 15 - Javauser3202835View Answer on Stackoverflow