弹簧引导 - 多部分文件最大上载大小异常
2022-09-04 21:20:06
我正在使用Spring Boot 2.1.3(使用标准的Tomcat嵌入式Web服务器)开发一个端点来上传图像,我想限制分段上传的大小。我可以使用属性轻松执行此操作:
spring:
servlet:
multipart:
max-file-size: 2MB
max-request-size: 2MB
但是我总是得到一个Spring无法捕获的500,因为是Tomcat抛出异常,并且请求没有到达我在RestController中的代码。
2019-03-02 10:12:50.544 ERROR [] [ o.a.c.c.C.[.[.[/].[dispatcherServlet]] [] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (3917946) exceeds the configured maximum (2097152)
我的异常处理程序是这样的,但显然无论在注释中放入什么异常都不起作用:
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity handleMaxSizeException(Exception e) {
logger.warn(e.getMessage());
...
return status(HttpStatus.PAYLOAD_TOO_LARGE).body(...);
}
我已经尝试了已经提到的属性,但没有任何影响:
@Bean
public TomcatServletWebServerFactory containerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector ->
((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1));
return factory;
}
我已经检查了以下问题(以及其他问题)中的答案,但它们大多已经过时或根本不起作用:
- 多部分文件最大大小异常 - 弹簧启动浮雕雄猫
- MaxUploadSizeExceedException在Spring中不调用异常处理方法
- 如何处理MaxUploadSizeExceedException
有人在为此苦苦挣扎吗?