在Spring Boot中配置上传文件大小限制有两种方法:
- application.properties/application.yml文件中配置: 可以在application.properties或application.yml文件中添加如下配置来限制上传文件的大小:
# 设置最大文件上传大小 spring.servlet.multipart.max-file-size=10MB # 设置最大请求大小 spring.servlet.multipart.max-request-size=10MB
- 使用Java配置类配置: 创建一个配置类,继承WebMvcConfigurerAdapter,并重写configure方法:
@Configuration public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/uploads/**") .addResourceLocations("file:/path/to/upload/folder/") .setCachePeriod(0); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/uploads/**") .addResourceLocations("file:/path/to/upload/folder/") .setCachePeriod(0); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } }
以上两种方法可以实现对上传文件大小的限制。