2015-10-20 690 views
19

我有一个相当简单的Spring Boot Web应用程序,我有一个带有enctype="multipart/form-data"表单的HTML页面。我得到这个错误:在Spring Boot中增加HTTP Post 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兆字节。有什么方法可以编辑这个值吗?通过application.properties这样做是最好的,而不是必须创建自定义的bean或混淆XML文件。

+0

无论你声明你的多部分解析器,你可以改变这些东西。 –

+0

我没有在任何地方宣布它。我假设这意味着春季启动会自动创建并处理它。这是否意味着我无法编辑它? – Jordan

+0

您可以编辑它。找到多部分解析器并编辑该值。如果有多部分支持,我相信你会找到它的一些配置。你甚至没有在你的主帖中发布你的配置,所以没有人可以指出要改变什么。 –

回答

11

找到了解决方案。将此代码添加到运行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,但我还没有尝试过自己,所以我无法证实。

multipart.maxFileSize=10Mb # Max file size. 
multipart.maxRequestSize=10Mb # Max request size. 
+0

添加属性适用于我:multipart.maxFileSize和multipart.maxRequestSize –

+0

“连接器”解决方案对我而言并不适用,也不适用于我'multipart.maxFileSize'。对我来说有效的是以下答案中的'spring.http.multipart.max-file-size'。 – Riki137

14

application.properties文件这样写:

# Max file size. 
spring.http.multipart.max-file-size=1Mb 
# Max request size. 
spring.http.multipart.max-request-size=10Mb 

根据您的需要调整大小。

+0

试过,仍然给我错误。我实际上并没有上传表单中的任何文件,我只是有很多输入字段,数值长达数万个字符,如果我的文本太多(超过2MB的文本),它会崩溃。希望将postMaxSize增加到2MB以避免这个问题。 – Jordan

+0

可以请你发布你的代码和错误stacktrace –

+1

破折号不应该包含在属性名称中。 multipart.maxRequestSize = 20MB multipart.maxFileSize = 20MB – userM1433372

1

从文档: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.

2

有一些差异,当我们定义在application.properties和应用YAML属性。

在application.yml:

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

而在application.propeties:

spring.http.multipart.max文件大小= 128KB spring.http.multipart.max-请求 - 大小= 128KB

注:Spring版本4.3和Spring 1.4的引导

1

首先,确保你使用的spring.servlet代替spring.http

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

如果你有使用Tomcat,你可能最终创造EmbeddedServletContainerCustomizer,这是不是非常好的事情。

如果你可以没有托马,你可以replace tomcat with e.g. undertow,并避免这个问题。

+0

Undertow具有相同的问题,因此切换不能避免此问题。这个答案中的配置可以在Undertow中使用最新版本的Spring-Boot v1.5.9 –