spring boot配置对上传文件的大小限制
spring-boot 2.0之前
Spring Boot1.4版本后配置更改为(默认单个文件最大1Mb,单次请求文件总数大小最大10Mb):
spring.http.multipart.maxFileSize = 20MB
spring.http.multipart.maxRequestSize=20MB
spring-boot 2.0之后:
Spring Boot2.0之后的版本配置修改为(默认单个文件最大1Mb,单次请求文件总数大小最大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,然后手动设置临时路径,原文:https://www.snowruin.com/?p=1728
具体如下:
@Bean MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); factory.setLocation("D:\aa"); return factory.createMultipartConfig(); }
这样应该可以,但我认为既然spring-boot给了配置项,就尽量不要重写bean
我觉得应该是我配置spring.http.multipart.location=D:\aa 这个有问题了
试啊试,当我把spring.http.multipart.location=D:\aa 改成spring.servlet.multipart.location=D:\aa,竟然好了