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:52

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.

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
server.tomcat.max-http-post-size=100000000
server.tomcat.max-swallow-size=100000000

Working with Spring Boot 2.0.5.RELEASE

查看更多
神经病院院长
3楼-- · 2020-01-25 06:54

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));
查看更多
倾城 Initia
4楼-- · 2020-01-25 06:57

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.

查看更多
我想做一个坏孩纸
5楼-- · 2020-01-25 06:57

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" />
查看更多
登录 后发表回答