Home >Java >javaTutorial >How to solve the problem of limited upload file size in SpringBoot
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. at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:121) at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:114)
As can be seen from the above exception, the reason is that the uploaded file exceeds the maximum value of spring's default configuration of 1048576 bytes. We usually use the MultipartFile interface class to upload files. When receiving files uploaded by the front end, it can be seen that the default limit for MultipartFile files is also 1048576 bytes, which is 1M.
But in many cases, the ID photos or documents we take directly with our mobile phones are basically above 2M. Obviously this cannot meet our daily needs, so we have to modify the default configuration parameter size. .
Different versions of SpringBoot have different corresponding setting parameters:
Spring Boot 1.3.x and earlier
multipart.maxFileSize
multipart.maxRequestSize
Spring Boot 1.4.x and 1.5 .x
spring.http.multipart.maxFileSize
spring.http.multipart.maxRequestSize
Spring Boot 2.x
spring.servlet.multipart.maxFileSize
spring.servlet.multipart .maxRequestSize
For example, I The version of SpringBoot 2.1.3 is used, and then the parameter size is set directly in the configuration file:
#做限制的参数配置 spring: servlet: multipart: enabled: true #默认支持文件上传 max-file-size: 20MB # 最大支持文件大小 max-request-size: 30MB # 最大支持请求大小 #不做限制的参数配置 spring: servlet: multipart: enabled: true #默认支持文件上传 max-file-size: -1 #不做限制 max-request-size: -1 #不做限制
After setting, restart the project and the file can be uploaded successfully.
Configure the parameters in the remote configuration file center. If it is in the configuration file in the configuration project, it is the same as method 1. There is no need to write a separate configuration class anymore. The parameters are configured in the remote configuration center so that the parameters can be dynamically modified according to temporary needs without restarting the project.
Common remote configuration file center services include Nacos, Apollo (Apollo), SpringCloud, etc. I use the Nacos configuration center service:
Custom MultipartFileConfigConfiguration class:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.unit.DataSize; import javax.servlet.MultipartConfigElement; /** * @author: 一宿君 * @date: 2022-03-23 19:18:51 * @description: */ @Configuration public class MultipartFileConfig { @Value("${config.multifile.maxFileSize}") private Long maxFileSize; @Value("${config.multifile.maxRequestSize}") private Long maxRequestSize; @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); /** * 单个数据大小, * DataSize.ofMegabytes(maxFileSize)默认是配置字节,将字节转化为MB */ factory.setMaxFileSize(DataSize.ofMegabytes(maxFileSize)); // 总上传数据大小 factory.setMaxRequestSize(DataSize.ofMegabytes(maxRequestSize)); return factory.createMultipartConfig(); } }
So you can control the size of the uploaded file at any time!
The above is the detailed content of How to solve the problem of limited upload file size in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!