【问题标题】:Confusion regarding spring.http.multipart.max-file-size vs spring.servlet.multipart.max-file-size关于 spring.http.multipart.max-file-size 与 spring.servlet.multipart.max-file-size 的混淆
【发布时间】:2019-10-06 22:27:36
【问题描述】:

我已经浪费了几天时间让 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 控制器方法一起使用,即像这样...... @GetMapping("/files/{文件名:.+}") @ResponseBody 公共模型和查看你的方法(.....) 或者这根本不需要,并且是一个完整的红鲱鱼,它是下面的设置为大于 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 file-upload application.properties


    【解决方案1】:

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

    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
    

    最大文件大小与最大请求大小

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

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

    还支持MB或KB后缀; 默认1MB


    spring.servlet.multipart.max-request-size=10MB 
    

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

    也支持MB或KB后缀

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


    更新: 您不需要在控制器级别指定任何 spring.** property(在某些情况下需要标头 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.http.** 等开头的属性名称等没有效果,并且在纯 REST 中不起作用,例如上面我的问题中提到的带有以下注释的请求.
      即...
      @GetMapping("/files/{filename:.+}")
      @ResponseBody
      对吗?
    • 是的,GetMapping(...) 用于使用 GET 方法映射给定的 URL,如果您使用的是 RestController,则甚至不需要使用 @ResponseBody
    • 道歉。我的意思是那些 @GetMapping(.....) 类似 REST 的注释不需要 spring.http.** 属性设置。事实上 spring.http.** 根本不需要。正确的? spring.servlet.** 属性设置是任何文件上传情况所需要的。对吗?
    • 谢谢你的回答,顺便说一句。
    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2013-11-23
    • 2021-01-27
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多