1修改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 */@Beanpublic MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("20480KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); 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() { MultipartConfigFactory factory = new MultipartConfigFactory(); //单个文件最大 factory.setMaxFileSize("20480KB"); //KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); } }}
原文地址:https://www.cnblogs.com/woshuaile/p/11600568.html
时间: 2024-10-10 23:38:01