使用springboot ,pom文件的parent配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.1.4版本的springboot,启动类配置了tomcat的setMaxSwallowSize也依旧出错,暂时不清楚高版本jar修改了什么,若有同学知道如何配置,希望您留言告知,或者我这边配置有问题,希望告知。2.0.5配置之后,则不会出错。
启动类中tomcat配置如下。
//Tomcat large file upload connection reset
@Bean
public TomcatServletWebServerFactory tomcatEmbedded() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
//-1 means unlimited
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
}
});
return tomcat;
}
配置文件application.properties
#支持的最大文件
spring.servlet.multipart.max-file-size=100MB
#文件请求最大限制
spring.servlet.multipart.max-request-size=100MB
全局异常类捕获
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MultipartException.class)
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
System.out.println("aaa");
return "redirect:/uploadStatus";
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
@ResponseBody
public String handleError2(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
System.out.println("bbb");
return "failed";
}
}
此方法是参考的其他博主的,2.0.5版本可以使用,但2.1.4依旧出错。
参考的地址
http://www.cnblogs.com/ityouknow/p/8298344.html
https://www.mkyong.com/spring-boot/spring-boot-configure-maxswallowsize-in-embedded-tomcat/