5

我已经设置最大文件大小多部分文件的最大尺寸异常 - 春季启动embbeded tomcat的

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

这是我的控制器:

@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

EDIT 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); 
} 

}

+0

您未显示足够的代码。例如,'handleExceptionInternal'是做什么的。一个[最小的,完整的,可验证的例子](http://stackoverflow.com/help/mcve)将使人们更容易帮助你。 –

+0

'handleExceptionInternal'是'ResponseEntityExceptionHandler'的内部方法,它是通过“希望提供集中式异常处理的{Controller LinkAdvice @ControllerAdvice}类 *的便利基类”。 –

回答

10

这是棘手。 Tomcat属性MaxSwallowSize导致了这个问题。显然它是在最近的Tomcat版本中引入的。它背后的全部想法是,如果Tomcat意识到请求将被拒绝,终止高于默认2mb的连接(至少这是我的解释)。简单的覆盖这个属性修复了一些东西。我意识到这不是完美的解决方案,但它比仅仅终止连接好得多。

@Bean 
public TomcatEmbeddedServletContainerFactory containerFactory() { 
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { 
     @Override 
     public void customize(Connector connector) { 
     ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1); 
     } 
    }); 
    return factory; 
} 
+0

你刚刚在帖子中保存了本质上相同的代码。我怀疑我会发现答案,如果有的话,没有很多小时的麻烦,所以谢谢你,谢谢你,谢谢。 –

+1

@TimPerry不客气。总是乐于帮助:) –

+0

谢谢你,你钉了它,它应该成为Springboot文档的一部分。 – lekant

0

我写application.yml一些行来解决这个问题,如:

spring: 
    http: 
     multipart: 
      max-file-size: 10MB 
      max-request-size: 10MB 

它帮助而我在appl写道ication.properties但不在yml中。

+0

这篇文章不是回答这个问题的实际尝试。请注意[Stack Overflow不像讨论论坛](http://stackoverflow.com/about),它是一个问答网站,每个帖子都是问题或问题的答案。帖子也可以有[评论](http://stackoverflow.com/help/privileges/comment) - 这样的小句子 - 可以用来批评或请求作者澄清。这应该是一个评论或[新问题](http://stackoverflow.com/questions/ask)。 –

+0

我编辑删除您的答案中的问题,因为答案只能提供答案。您可以将该问题作为评论或作为新问题发布。 –