Java 在 Spring Boot 中增加 HTTP Post maxPostSize
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33232849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Increase HTTP Post maxPostSize in Spring Boot
提问by Jordan
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:
我有一个相当简单的 Spring Boot Web 应用程序,我有一个带有enctype="multipart/form-data"
. 我收到此错误:
The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.
多部分请求包含超出关联连接器上设置的 maxPostSize 限制的参数数据(不包括上传的文件)。
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.
我正在使用 Spring Boot 的默认嵌入式 tomcat 服务器。显然,默认maxPostSize
值是 2 兆字节。有没有办法编辑这个值?通过这样做application.properties
是最好的,而不必创建自定义的 bean 或弄乱 xml 文件。
采纳答案by Jordan
Found a solution. Add this code to the same class running SpringApplication.run.
找到了解决办法。将此代码添加到运行 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.
编辑:显然将它添加到您的 application.properties 文件也会增加 maxPostSize,但我自己没有尝试过,所以我无法确认。
multipart.maxFileSize=10Mb # Max file size.
multipart.maxRequestSize=10Mb # Max request size.
回答by Sanjay Singh Rawat
In application.propertiesfile write this:
在application.properties文件中这样写:
# 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
注意:从Spring Boot 2 开始,你现在可以做
# Max file size.
spring.servlet.multipart.max-file-size=1MB
# Max request size.
spring.servlet.multipart.max-request-size=10MB
回答by Dániel Kis
from documentation: https://spring.io/guides/gs/uploading-files/
来自文档: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.
调整文件上传限制
在配置文件上传时,设置文件大小限制通常很有用。想象一下尝试处理 5GB 的文件上传!使用 Spring Boot,我们可以使用一些属性设置来调整其自动配置的 MultipartConfigElement。
将以下属性添加到您现有的
src/main/resources/application.properties
:
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB
多部分设置的约束如下:
spring.http.multipart.max-file-size is set to 128KB
,表示总文件大小不能超过 128KB。
spring.http.multipart.max-request-size
设置为 128KB,这意味着 multipart/form-data 的总请求大小不能超过 128KB。
回答by denzal
There is some difference when we define the properties in the application.yaml
and application.properties
.
当我们在application.yaml
和 中定义属性时有一些区别application.properties
。
In application.yml
:
在application.yml
:
spring:
http:
multipart:
max-file-size: 256KB
max-request-size: 256KB
And in application.propeties
:
并在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
注意:Spring 4.3 和 Spring boot 1.4
回答by Ondrej Kvasnovsky
First, make sure you are using spring.servlet
instead of spring.http
.
首先,确保您使用的是spring.servlet
而不是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.
如果您必须使用 tomcat,您可能最终会创建EmbeddedServletContainerCustomizer,这并不是一件好事。
If you can live without tomat, you could replace tomcat with e.g. undertowand avoid this issue at all.
如果你可以不用 tomat 也能生活,你可以用例如 undertow 替换 tomcat并完全避免这个问题。
回答by Fabi Ko
If you are using using x-www-form-urlencodedmediatype 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:
如果您在 POST 请求中使用x-www-form-urlencoded媒体类型(就像我一样),则 spring-boot 的 multipart 属性不起作用。如果您的 spring-boot 应用程序也启动了一个 tomcat,您需要在 application.properties 文件中设置以下属性:
# 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-urlencodedencoding of the body.
我在 spring-boot 文档中的任何地方都找不到该信息。希望它可以帮助那些也坚持使用x-www-form-urlencoded编码的人。
回答by spiritworld
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)
没有一个解决方案对我有用,而且它们中的大多数只是彻头彻尾的离题,因为 OP 正在谈论maxPostSize,而不是maxFileSize(如果超过大小,后者无论如何都会给你一个不同的错误)
Solution: in /tomcat/conf/server.xml add maxPostSize="" attribute to Connector
解决方法:在/tomcat/conf/server.xml 中给Connector 添加maxPostSize="" 属性
<!-- 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" />
回答by Steve Banton
Apply settings for Tomcat as well as servlet
应用 Tomcat 和 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.
您可以为 Tomcat 设置最大帖子大小,application.properties
其中使用 int 设置,如下所示。仅设置spring.servlet.multipart.max-request-size=10MB
,就像在其他一些答案中一样,可能还不够。
# 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
使用 Spring Boot 2.0.5.RELEASE
回答by user11999314
For me nothing of previous works (maybe use application with yaml is an issue here), but get ride of that issue using that:
对我来说,以前的作品没有任何东西(也许在这里使用带有 yaml 的应用程序是一个问题),但是使用它来解决这个问题:
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();
}
}
回答by Dev_1979
This worked for me with Tomcat 8
这对我的 Tomcat 8 有用
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Catalina:type=Connector,port=" + 8080);
mbeanServer.setAttribute(objectName, new Attribute("maxPostSize", 100000000));