spring boot配置对上传文件的大小限制
spring boot配置對上傳文件的大小限制
spring-boot 2.0之前
Spring Boot1.4版本后配置更改為(默認單個文件最大1Mb,單次請求文件總數(shù)大小最大10Mb):
spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize=20MB
spring-boot 2.0之后:
Spring Boot2.0之后的版本配置修改為(默認單個文件最大1Mb,單次請求文件總數(shù)大小最大10Mb):
spring.servlet.multipart.max-file-size = 20MB
spring.servlet.multipart.max-request-size=20MB
超過20Mb,就會報錯,如下:
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (25766453) exceeds the configured maximum (20971520)
還有其他一些配置,都是spring.http.改為spring.servlet.
1.啟動報錯:
APPLICATION FAILED TO START
Description:
Failed to bind properties under ‘spring.servlet.multipart.max-file-size’ to org.springframework.util.unit.DataSize:
Property: spring.servlet.multipart.max-file-size Value: 20Mb Origin: "spring.servlet.multipart.max-file-size" from property source "application" Reason: failed to convert java.lang.String to org.springframework.util.unit.DataSize解決:
spring.http.multipart.maxFileSize = 20Mb 改為 spring.http.multipart.maxFileSize = 20MB
及Mb改為MB
或者,就寫spring.http.multipart.maxFileSize=20,不要加單位,也是對的,唯獨加Mb就會報錯
2.上傳報錯:
The temporary upload location [C:\Users(你的路徑).8080\work\Tomcat\localhost\ROOT] is not valid
我的spring-boot版本是2.1.1
配置臨時路徑:
spring.http.multipart.location= D:\aa
我的D盤也有這個aa的文件夾,但就是報錯
百度說要增加一個bean,然后手動設(shè)置臨時路徑,原文:https://www.snowruin.com/?p=1728
具體如下:
這樣應(yīng)該可以,但我認為既然spring-boot給了配置項,就盡量不要重寫bean
我覺得應(yīng)該是我配置spring.http.multipart.location=D:\aa 這個有問題了
試啊試,當我把spring.http.multipart.location=D:\aa 改成spring.servlet.multipart.location=D:\aa,竟然好了
總結(jié)
以上是生活随笔為你收集整理的spring boot配置对上传文件的大小限制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GCD简介三:Dispatch Sour
- 下一篇: java8新特性(3)--- 函数式接口