springboot上传文件过大的500异常解决
人气:0修改appliaction.properties
# 单个文件最大20m spring.servlet.multipart.max-file-size=20MB #一次请求最大100M spring.servlet.multipart.max-request-size=100MB
如果配置文件为appliaction.yml的这样配置文件:
spring: servlet: multipart: maxFileSize: 20MB maxRequestSize: 100MB
500代码异常,在启动类的里追加
/** * 文件上传配置 * * @return */ @Bean public MultipartConfigElement multipartConfigElement() { /* springBoot 1.0+ 推荐这种写法 MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("20480KB"); //KB,MB factory.setMaxFileSize("20480KB"); /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); */ // springBoot 2.0+ 推荐这种写法, MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES)); /// 设置总上传数据总大小10M factory.setMaxRequestSize(DataSize.of(100, DataUnit.MEGABYTES)); return factory.createMultipartConfig(); }
这是我的启动类:
package com.tythin.tyboot.rest; import org.apache.coyote.http11.AbstractHttp11Protocol; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import com.tythin.tyboot.core.config.WebAutoConfiguration; import javax.servlet.MultipartConfigElement; @SpringBootApplication(exclude = { WebAutoConfiguration.class }) @EnableScheduling @MapperScan(basePackages = "XXXX") public class TybootRestApplication { public static void main(String[] args) { SpringApplication.run(TybootRestApplication.class, args); } @Configuration public class CorsConfig { private CorsConfiguration buildConfig() { CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*"); // 1允许任何域名使用 corsConfiguration.addAllowedHeader("*"); // 2允许任何头 corsConfiguration.addAllowedMethod("*"); // 3允许任何方法(post、get等) return corsConfiguration; } @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", buildConfig()); // 4 return new CorsFilter(source); } /** * 文件上传配置 * * @return */ @Bean public MultipartConfigElement multipartConfigElement() { /* springBoot 1.0+ 推荐这种写法 MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("20480KB"); //KB,MB factory.setMaxFileSize("20480KB"); /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); */ // springBoot 2.0+ 推荐这种写法, MultipartConfigFactory factory = new MultipartConfigFactory(); //文件最大10M,DataUnit提供5中类型B,KB,MB,GB,TB factory.setMaxFileSize(DataSize.of(10, DataUnit.MEGABYTES)); /// 设置总上传数据总大小10M factory.setMaxRequestSize(DataSize.of(100, DataUnit.MEGABYTES)); return factory.createMultipartConfig(); } } }
您可能感兴趣的文章:
加载全部内容