在Spring Boot中增加HTTP Post maxPostSize [英] Increase HTTP Post maxPostSize in Spring Boot
问题描述
我有一个相当简单的Spring Boot Web应用程序,我只有一个HTML页面,其表单带有enctype="multipart/form-data"
.我收到此错误:
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:
多部分请求包含的参数数据(不包括上传的文件)超出了在关联的连接器上设置的maxPostSize的限制.
The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector.
我正在使用Spring Boot的默认嵌入式tomcat服务器.显然,默认的maxPostSize
值为2 MB.有什么办法可以编辑此值?最好通过application.properties
这样做,而不是必须创建自定义bean或将xml文件弄乱.
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.
推荐答案
找到了解决方案.将此代码添加到运行SpringApplication.run的同一类中.
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
}
);
}
};
}
显然,将其添加到您的application.properties文件中也会增加maxPostSize,但我自己尚未尝试过,因此无法确认.
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.
这篇关于在Spring Boot中增加HTTP Post maxPostSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!