spring-boot - 关于 spring.http.multipart.max-file-size 与 spring.servlet.multipart.max-file-size 的混淆

标签 spring-boot file-upload application.properties

我花了几天时间让 Spring Boot Upload 文件工作,但是,和 Spring 一样,你不知道魔法是如何工作的,即使在使用这个框架多年之后 - 你必须用谷歌搜索大量时间来解开哪里出了问题并解决诸如穿越迷宫之类的事情,这是可维护性的噩梦。

使用 Spring Boot 2.2.0.M3 进行文件上传 2 对设置之间有什么区别?哪个是对的 ?

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

上面的“http”是否与 Spring REST Controller 方法一起使用,就像这样......
@GetMapping("/files/{文件名:.+}")
@ResponseBody
public ModelAndView yourMethod(.....)
或者这根本不需要,它是一个完整的红鲱鱼,它是下面的设置,它为 REST http 或 Servlet 请求大于默认值 1MB 的文件完成所有工作。

spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

上传异常

超过最大上传大小;嵌套异常是 java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 字段文件超出其最大允许大小 1048576 字节。

最佳答案

他们在不同版本中更改了属性名称。

Spring Boot 1.3.x 及更早版本

multipart.max-file-size
multipart.max-request-size

Spring Boot 1.3.x 之后:
spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

Spring Boot 2.0 之后:
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1

max-file-size Vs max-request-size



spring.servlet.multipart.max-file-size = 2MB

上传支持的每个文件的最大大小为 2MB;

还支持 MB 或 KB 后缀;
默认 1MB
spring.servlet.multipart.max-request-size=10MB 

整个请求的最大大小为 10MB;

还支持 MB 或 KB 后缀

对于无限上传文件大小,似乎设置-1将使其无限文件大小。

更新:
您不需要指定任何 spring.** property在 Controller 级别(在某些情况下期望 header Content-Type)。您可以在 appilcation.properties 中设置这些属性文件如下。
# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0B # Threshold after which files are written to disk.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
spring.servlet.multipart.max-file-size=1MB # Max file size.
spring.servlet.multipart.max-request-size=10MB # Max request size.
spring.servlet.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

关于spring-boot - 关于 spring.http.multipart.max-file-size 与 spring.servlet.multipart.max-file-size 的混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56222833/

相关文章:

java - 使用 <t :inputFileUpload> 时出现问题

asp.net - Visual Basic最大文件大小错误

PHP cURL - 如何通过 HTTP 请求将变量的内容作为文件上传?

多模块项目中的Spring Boot i18n

java - Spring Boot 使用 SSL 连接到 Postgresql

java - Spring Boot 无法从数据源确定 jdbc url

spring-boot - 使用sync=true的@Cacheable方法如何处理异常

spring-boot - 使用 spring boot 属性文件设置 Flyway 'baselineOnMigrate' 和 'baselineVersion'

spring-boot - 带有 primefaces 的 spring boot 给出错误 : java. lang.NoClassDefFoundError: org/springframework/core/log/LogMessage

java - 使用 @OneToMany 或 @ManyToMany 定位未映射的类(多数据库连接)