MaxUploadSizeExceededException: Maximum upload size exceeded
1、场景
上传文件,文件大小1.7M,请求接口报错
接口:
@PostMapping("/import")
@ResponseBody
public FormatResponse imports(@RequestParam("file") MultipartFile file) {
/**省略业务代码**/
}
2、定位原因
查看日志:
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.
查看源码知:默认maxFileSize为1MB
@ConfigurationProperties(prefix = "spring.servlet.multipart", ignoreUnknownFields = false)
public class MultipartProperties {
/**
* Whether to enable support of multipart uploads.
*/
private boolean enabled = true;
/**
* Intermediate location of uploaded files.
*/
private String location;
/**
* Max file size. Values can use the suffixes "MB" or "KB" to indicate megabytes or
* kilobytes, respectively.
*/
private String maxFileSize = "1MB";
/**
* Max request size. Values can use the suffixes "MB" or "KB" to indicate megabytes or
* kilobytes, respectively.
*/
private String maxRequestSize = "10MB";
/**
* Threshold after which files are written to disk. Values can use the suffixes "MB"
* or "KB" to indicate megabytes or kilobytes, respectively.
*/
private String fileSizeThreshold = "0";
/**
* Whether to resolve the multipart request lazily at the time of file or parameter
* access.
*/
private boolean resolveLazily = false;
}
3、解决方案:
配置maxFileSize参数
spring:
servlet:
multipart:
maxFileSize: 2MB
配置完成本地成功上传1.7M文件(localhost)
4、环境运行
部署测试环境上传失败
Response Headers如下:Request Entity Too Large
HTTP/1.1 413 Request Entity Too Large date: Mon, 01 Mar 2021 09:20:53 GMT content-type: text/html content-length: 578
联系运营修改域名配置
上一篇:
通过多线程提高代码的执行效率例子
