springboot上传⽂件过⼤的500异常解决修改appliaction.propertiesspringframework和springboot
# 单个⽂件最⼤20m
spring.servlet.multipart.max-file-size=20MB
#⼀次请求最⼤100M
spring.servlet.multipart.max-request-size=100MB
如果配置⽂件为l的这样配置⽂件:
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");
ateMultipartConfig();
*/
// 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));
ateMultipartConfig();
}
这是我的启动类:
st;
import http11.AbstractHttp11Protocol;
batis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.at.TomcatConnectorCustomizer;
import org.springframework.at.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.s.CorsConfiguration;
import org.s.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
onfig.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();
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");
ateMultipartConfig();
*/
// 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));
ateMultipartConfig();
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。