Java SpringBoot 的 @MultipartConfig maxFileSize 不生效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36945482/
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
SpringBoot's @MultipartConfig maxFileSize not taking effect
提问by Nic Wolfe
I have a controller with a MultipartConfig
annotation (a snippet of which is show below):
我有一个带MultipartConfig
注释的控制器(下面显示了其中的一个片段):
@RestController
@RequestMapping("packages")
@MultipartConfig(maxFileSize = 1024*1024*1024, maxRequestSize = 1024*1024*1024)
public class PackagesController
{
@RequestMapping(method = RequestMethod.POST)
public String create(@RequestParam("package") MultipartFile uploadedPackage)
{
// do stuff to the file
return "create";
}
}
When I upload a file to this endpoint, though, it appears to be using the default multipart config values:
但是,当我将文件上传到此端点时,它似乎使用了默认的多部分配置值:
Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field package exceeds its maximum permitted size of 1048576 bytes.
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl.raiseError(FileUploadBase.java:633) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_45]
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:293) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
at org.apache.catalina.connector.Request.parseParts(Request.java:2776) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
... 30 common frames omitted
My application looks like this:
我的应用程序如下所示:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
Is there something I'm missing that allows the @MultipartConfig
annotation to take effect?
有什么我遗漏的东西可以让@MultipartConfig
注释生效吗?
采纳答案by Ali Dehghani
If you just want to control the multipart properties, you can use multipart.max-file-size
and multipart.max-request-size
properties. For example, you could raise the max size to 100Mb
by adding following piece of configurations in your application.properties
file:
如果只想控制多部分属性,可以使用multipart.max-file-size
和multipart.max-request-size
属性。例如,您可以100Mb
通过在application.properties
文件中添加以下配置来提高最大大小:
multipart.max-file-size=100MB
multipart.max-request-size=100MB
Values can use the suffixed MB
or KB
to indicate a Megabyteor Kilobytesize.
值可以使用后缀MB
或KB
来指示兆字节或千字节大小。
Under the hood, Spring Boot will create a MultipartConfigElement
based on MultipartProperties
and that MultipartConfigElement
will be used in Servlet registration, as stated in Spring MVC documentation. You can take a look at MultipartAutoConfiguration
and DispatcherServletConfiguration
and Checkout Spring Boot documentationfor more information.
在底层,Spring Boot 将创建一个MultipartConfigElement
基于MultipartProperties
,MultipartConfigElement
并将用于 Servlet 注册,如Spring MVC 文档中所述。你可以看看MultipartAutoConfiguration
,并DispatcherServletConfiguration
和Google Checkout春季启动文档以获取更多信息。
回答by tbo47
With spring-boot 1.5.3 you should use the following code in application.yml
使用 spring-boot 1.5.3 你应该在 application.yml 中使用以下代码
spring:
http:
multipart:
max-file-size: 100MB
max-request-size: 100MB
Make sure to use spaces and not tab in your yaml file.
确保在 yaml 文件中使用空格而不是制表符。
回答by cst1992
For setting custom limits for multipart uploads, the below properties are to be used(for a sample size of 30MB):
要为分段上传设置自定义限制,将使用以下属性(样本大小为 30MB):
spring.http.multipart.max-file-size=30MB
spring.http.multipart.max-request-size=30MB
In our company's projects, I found that one of them was on Spring Boot version 1.3.5, so for versions < 1.4 you should use
在我们公司的项目中,我发现其中一个是在 Spring Boot 1.3.5 版上,因此对于 <1.4 版,您应该使用
multipart.max-file-size=30MB
multipart.max-request-size=30MB
From the docs(v1.4.0):
Spring Boot embraces the Servlet 3
javax.servlet.http.Part
API to support uploading files. By default Spring Boot configures Spring MVC with a maximum file of 1MB per file and a maximum of 10MB of file data in a single request. You may override these values, as well as the location to which intermediate data is stored (e.g., to the/tmp
directory) and the threshold past which data is flushed to disk by using the properties exposed in theMultipartProperties
class. If you want to specify that files be unlimited, for example, set thespring.http.multipart.max-file-size
property to-1
.The multipart support is helpful when you want to receive multipart encoded file data as a
@RequestParam
-annotated parameter of typeMultipartFile
in a Spring MVC controller handler method.
Spring Boot 包含 Servlet 3
javax.servlet.http.Part
API 来支持上传文件。默认情况下,Spring Boot 将 Spring MVC 配置为每个文件的最大文件为 1MB,单个请求中的文件数据最大为 10MB。您可以覆盖这些值,以及存储中间数据的位置(例如,到/tmp
目录)和通过使用MultipartProperties
类中公开的属性将数据刷新到磁盘的阈值。例如,如果要指定文件不受限制,请将spring.http.multipart.max-file-size
属性设置 为-1
.当您希望在 Spring MVC 控制器处理程序方法中将多部分编码的文件数据作为
@RequestParam
类型的带注释参数 接收时,多部分支持很有用MultipartFile
。
Same docs for version 1.3.8:
版本 1.3.8 的相同文档:
If you want to specify that files be unlimited, for example, set the
multipart.maxFileSize
property to-1
.
例如,如果要指定文件不受限制,请将
multipart.maxFileSize
属性设置 为-1
.
回答by T4keo
With Spring Boot 2.0, you should use this in your application.yml
使用 Spring Boot 2.0,您应该在 application.yml 中使用它
spring:
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB
From documentation:
从文档:
Spring Boot embraces the Servlet 3
javax.servlet.http.Part
API to support uploading files. By default, Spring Boot configures Spring MVC with a maximum size of 1MBper file and a maximum of 10MBof file data in a single request. You may override these values, the location to which intermediate data is stored (for example, to the/tmp
directory), and the threshold past which data is flushed to disk by using the properties exposed in theMultipartProperties
class. For example, if you want to specify that files be unlimited, set the spring.servlet.multipart.max-file-sizeproperty to-1
.
Spring Boot 包含 Servlet 3
javax.servlet.http.Part
API 来支持上传文件。默认情况下,Spring Boot 将 Spring MVC 配置为每个文件的最大大小为1MB,单个请求中的文件数据最大为10MB。您可以覆盖这些值、存储中间数据的位置(例如,到/tmp
目录)以及使用MultipartProperties
类中公开的属性将数据刷新到磁盘的阈值。例如,如果要指定文件不受限制,请将spring.servlet.multipart.max-file-size属性设置为-1
。
Extracted from Appendix A of documentation
摘自文档的附录 A
spring.servlet.multipart.max-file-size=1MB# Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-request-size=10MB# Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.max-file-size=1MB# 最大文件大小。值可以使用后缀“MB”或“KB”分别表示兆字节或千字节。
spring.servlet.multipart.max-request-size=10MB# 最大请求大小。值可以使用后缀“MB”或“KB”分别表示兆字节或千字节。
回答by user1188867
For Spring Boot v2+ add the following to the application.properties:
对于 Spring Boot v2+,将以下内容添加到 application.properties:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=40MB
回答by theshadog
On deployment, where the values are inserted via jenkins script, no "-" is allowed. Use:
在部署时,通过 jenkins 脚本插入值,不允许使用“-”。用:
env:
- name: SPRING_SERVLET_MULTIPART_MAXREQUESTSIZE
value: "10MB"
- name: SPRING_SERVLE
value: "10MB"
回答by theshadog
Application property is one option. Another option is
应用程序属性是一种选择。另一种选择是
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("10MB");
factory.setMaxRequestSize("10MB");
return factory.createMultipartConfig();
}