[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 files exceeds its maximum permitted size of 1048576 bytes.] to ModelAndView: reference to view with name '/error'; model is {exception=Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field files exceeds its maximum permitted size of 1048576 bytes.}
大概意思就是说,上传的文件太大了,超过自身限制了
我在Controller里面还加了限制,打断点并没有走到这里来
if (file.isEmpty()) {
return Result.error("文件为空!");
}
// 上传文件
if (file.getSize() > 5 * 1024 * 1024) {
return Result.error("文件大小超出范围!");
}
The field files exceeds its maximum permitted size of 1048576 bytes.
1024*1024 = 1048576 = 1M
经过各种试验得出spring boot 默认的上传文件大小是1024b*1024b
实现方式1: 在spring boot的启动类中加入下面配置
/**
* 文件上传配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//单个文件最大 5M
factory.setMaxFileSize("5120KB"); //KB,MB
/// 设置总上传数据总大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}
实现方式2:在配置文件中加入配置
spring:
servlet:
multipart:
max-file-size: 2Mb
max-request-size: 2Mb
再测试一下就ok了
spring boot 2.0环境,之前的配置稍微有一点不同