Spring Boot 上传文件大小设置(报错)

2018-10-23 17:40:34.150  WARN 1100 --- [nio-8080-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 

这个意思就是上传的文件超过最大限制1M,需要更改这个限制

原先是这样设置的,结果失效了

# 最大支持文件大小
spring.http.multipart.max-file-size=100MB
# 最大支持请求大小
spring.http.multipart.max-request-size=100MB

对比官方文档设置没问题啊

# MULTIPART (MultipartProperties)
spring.http.multipart.enabled=true # Enable support of multi-part uploads.
spring.http.multipart.file-size-threshold=0 # Threshold after which files will be written to disk. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.location= # Intermediate location of uploaded files.
spring.http.multipart.max-file-size=1MB # Max file size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.max-request-size=10MB # Max request size. Values can use the suffixed "MB" or "KB" to indicate a Megabyte or Kilobyte size.
spring.http.multipart.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

后来发现我的项目中springboot由原来的1.5.3升级到2.0.5了。。看下了下2.0.5文档是酱紫的。。。

# MULTIPART (MultipartProperties)
spring.servlet.multipart.enabled=true # Whether to enable support of multipart uploads.
spring.servlet.multipart.file-size-threshold=0 # Threshold after which files are written to disk. Values can use the suffixes "MB" or "KB" to indicate megabytes or kilobytes, respectively.
spring.servlet.multipart.location= # Intermediate location of uploaded files.
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.resolve-lazily=false # Whether to resolve the multipart request lazily at the time of file or parameter access.

将spring.http替换成spring.servlet就可以了

SpringBoot版本查看https://docs.spring.io/spring-boot/docs/

这个错误通常发生在Spring Boot项目中处理multipart/form-data请求时,如果没有配置MultipartResolver来解析上传的文件部分。解决这个问题,你可以按照以下步骤操作: 1. 添加依赖:确保在你的pom.xml或build.gradle文件中添加了Spring Web Multipart相关的依赖。对于Maven,添加: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web.multipart</artifactId> </dependency> ``` 或者Gradle: ```groovy implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-validation' ``` 2. 配置MultipartConfig:在Spring Boot应用中,你需要创建一个`MultipartConfig` bean,告诉Spring如何处理multipart请求。例如,在Application类里添加: ```java @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void configureMultipart(MultipartConfigFactory factory) { // 设置最大上传文件大小和其他限制 factory.setMaxFileSize("50MB"); // 可自定义合适大小 factory.setMaxRequestSize("100MB"); } } ``` 3. 验证Controller:如果你的控制器接收multipart请求,确保它已经声明为`@PostMapping("/upload")`或类似,并且使用了`@RequestParam("file") MultipartFile file`这样的注解来接收文件。 4. 检查是否真正发送了multipart请求:确认客户端发送的请求头包含`Content-Type: multipart/form-data`。 如果以上步骤都做了还是出现问题,可能是某个地方的配置丢失或者有误。检查相关配置和日志信息以获取更准确的错误原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值