Maximum upload size exceeded

springboot上传文件报错信息

Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: 
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
the request was rejected because its size (5019) exceeds the configured maximum (1024)

错误信息意思 : 上传文件的大小是5019,而最大可以上传的size是1024.超出最大限制,所以无法上传成功

造成这种问题的原因:上传文件太大导致的。
错误信息中可以看出SpringBoot设置的上传文件大小不能超过1MB,而实际却上传了5MB大小的文件。5MB超过了限制的1MB,导致上传文件报异常

解决办法:修改springboot设置上传文件大小。
以application.prperties为例(不同的springboot版本,配置可能有些差异)

# 单个文件大小为5MB
spring.servlet.multipart.max-file-size = 5MB

# 总上传的数据大小5MB
spring.servlet.multipart.max-request-size = 5MB

如何查看springboot的版本?

在pom.xml中查看当前项目使用的springboot的版本,不同版本的设置是不一样的,最典型的是两个常用版本是1.5.x的版本和2.x版本。如图是我的springboot版本
在这里插入图片描述

springboot上传文件报错信息

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
The field file exceeds its maximum permitted size of 1048576 bytes.

造成这种问题的原因:当没有在application.prperties中设置文件长传的大小时,使用的是springboot默认的上传大小。当超过springboot默认上传文件大小,就会报此异常,解决办法同上。

springboot不同版本的配置

springboot 1.x配置

spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB

官方文档低版本1.x配置详解

# 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 2.x配置

spring.servlet.multipart.max-file-size=30Mb
spring.servlet.multipart.max-request-size=30Mb

或者

spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=20MB

官方文档高版本2.x配置详解

# 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.

当springboot版本不同,配置也不同。如果配置错了,也会报异常

场景:springboot版本是2.x, 但是application.prperties配置的是1.x的配置,如下:

# 单个文件大小
spring.http.multipart.max-file-size=100MB
# 总上传的数据大小
spring.http.multipart.max-request-size=100MB

这是springboot1.x的配置,而springboot是2.x版本,此时报错信息为:

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. 

造成这种错误的原因:springboot1.x的配置不使用于springboot2.x。将spring.http替换成spring.servlet就可以了

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值