spring - 多部分文件最大大小异常——spring boot 内嵌tomcat

标签 spring spring-boot multipartform-data multipart embedded-tomcat-8

我已将最大文件大小设置为

multipart.maxFileSize: 1mb
multipart.maxRequestSize: 1mb

这是我的 Controller :
@RequestMapping(method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Secured(Privileges.CAN_USER_READ)
public void create(@RequestParam("file")final MultipartFile file,Principal principal) throws IllegalStateException, IOException,MultipartException{

    medicalHistoryService.create(new MedicalHistory(file));
}

这是错误信息
2016-03-03 13:48:24.560  WARN 4992 --- [nio-8080-exec-1] h.c.w.RestResponseEntityExceptionHandler : Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (9288401) exceeds the configured maximum (1048576)

2016-03-03 13:48:25.545  WARN 4992 --- [nio-8080-exec-2] h.c.w.RestResponseEntityExceptionHandler : Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (9288401) exceeds the configured maximum (1048576)

请求超大文件后的最终结果是加载页面出现问题。我在堆栈跟踪中没有遇到任何其他错误,所以我有点被实际发生的事情困住了。哦,是的,我尝试了许多其他解决方案,例如注册过滤器、处理 ErrorController 中的异常。每次我都会得到相同的结果 - 服务器崩溃。

Tomcat crash

编辑 2

我的异常处理类:
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{

   // 413 MultipartException - file size too big 
@ExceptionHandler({MultipartException.class,FileSizeLimitExceededException.class,java.lang.IllegalStateException.class})
   public ResponseEntity<Object> handleSizeExceededException(final WebRequest request, final MultipartException ex) {
        //log.warn("413 Status Code. File size too large {}", ex.getMessage());
       log.warn(ex.getMessage());
       final ApiError apiError = message(HttpStatus.PAYLOAD_TOO_LARGE, ex);
       return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.PAYLOAD_TOO_LARGE, request);
   }
}

最佳答案

从 Spring Boot 2 开始
spring.servlet.multipart.max-file-size=128KBspring.servlet.multipart.max-request-size=128KB
docs

Spring Boot 1.x

属性应该是:spring.http.multipart.max-file-size=128KBspring.http.multipart.max-request-size=128KB
spring boot guides

关于spring - 多部分文件最大大小异常——spring boot 内嵌tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35748022/

相关文章:

node.js - 使用 Connect Multiparty 处理在 Node js 中中止的请求

java - Kafka无序消息场景处理

docker - 使用 Docker 映射 Compute Engine 中的端口

java - 从 servlet 调用 servlet

java - 首次登录时无法打开 Hibernate Session 进行事务处理

java - 如何查找我的 Spring Boot 应用程序每秒收到多少个请求?

spring - Web 应用程序异常 : javax. ws.rs.NotSupportedException:HTTP 415 不支持的媒体类型

java - IntelliJ : How to enable JPAQL validation without persistence. XML?

spring - 如何在 Spring Boot 中为 jackson-datatype-money 创建 Jackson 定制器?

java - Spring Data Neo4j 4 中的动态属性