org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded;
springboot默认设置的上传文件的大小是1MB。
springboot中的源码:
@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. */ private DataSize maxFileSize = DataSize.ofMegabytes(1); /** * Max request size. */ private DataSize maxRequestSize = DataSize.ofMegabytes(10); /** * Threshold after which files are written to disk. */ private DataSize fileSizeThreshold = DataSize.ofBytes(0); /** * Whether to resolve the multipart request lazily at the time of file or parameter * access. */ private boolean resolveLazily = false; public boolean getEnabled() { return this.enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public String getLocation() { return this.location; } public void setLocation(String location) { this.location = location; } public DataSize getMaxFileSize() { return this.maxFileSize; } public void setMaxFileSize(DataSize maxFileSize) { this.maxFileSize = maxFileSize; } public DataSize getMaxRequestSize() { return this.maxRequestSize; } public void setMaxRequestSize(DataSize maxRequestSize) { this.maxRequestSize = maxRequestSize; } public DataSize getFileSizeThreshold() { return this.fileSizeThreshold; } public void setFileSizeThreshold(DataSize fileSizeThreshold) { this.fileSizeThreshold = fileSizeThreshold; } public boolean isResolveLazily() { return this.resolveLazily; } public void setResolveLazily(boolean resolveLazily) { this.resolveLazily = resolveLazily; } /** * Create a new {@link MultipartConfigElement} using the properties. * @return a new {@link MultipartConfigElement} configured using there properties */ public MultipartConfigElement createMultipartConfig() { MultipartConfigFactory factory = new MultipartConfigFactory(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold); map.from(this.location).whenHasText().to(factory::setLocation); map.from(this.maxRequestSize).to(factory::setMaxRequestSize); map.from(this.maxFileSize).to(factory::setMaxFileSize); return factory.createMultipartConfig(); } }
/** * Obtain a {@link DataSize} representing the specified number of megabytes. * @param megabytes the number of megabytes, positive or negative * @return a {@link DataSize} */ public static DataSize ofMegabytes(long megabytes) { return new DataSize(Math.multiplyExact(megabytes, BYTES_PER_MB)); }
所以可以看出来,在Springboot中设置的上传的文件的大小默认是1MB。
由两种方法可以进行设置上传文件的大小:
第一种就是在配置文件中配置最大的上传值:
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
比如在2.0设置
- spring.servlet.multipart.maxFileSize=30MB
- spring.servlet.multipart.maxRequestSize=30M
不做限制
- spring.servlet.multipart.maxFileSize=-1
- spring.servlet.multipart.maxRequestSize=-1
另一种方法
写一个config类
import javax.servlet.MultipartConfigElement; 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; @Configuration public class TomcatConfig { @Value("${spring.http.server.maxFileSize}") private Long maxFileSize; @Value("${spring.http.server.maxRequestSize}") private Long maxRequestSize; @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); // 单个数据大小 // factory.setMaxFileSize(MaxFileSize); // KB,MB factory.setMaxFileSize(DataSize.ofMegabytes(maxFileSize)); /// 总上传数据大小 factory.setMaxRequestSize(DataSize.ofMegabytes(maxRequestSize)); // factory.setMaxRequestSize(MaxRequestSize); return factory.createMultipartConfig(); }
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/p/14110653.html