springboot怎么配置上传文件大小

avatar
作者
筋斗云
阅读量:0

在Spring Boot中配置上传文件大小限制有两种方法:

  1. application.properties/application.yml文件中配置: 可以在application.properties或application.yml文件中添加如下配置来限制上传文件的大小:
# 设置最大文件上传大小 spring.servlet.multipart.max-file-size=10MB # 设置最大请求大小 spring.servlet.multipart.max-request-size=10MB 
  1. 使用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);     }  } 

以上两种方法可以实现对上传文件大小的限制。

    广告一刻

    为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!