分段文件最大大小异常-Spring Boot嵌入tomcat [英] Multipart file maximum size exception - spring boot embbeded tomcat

查看:111
本文介绍了分段文件最大大小异常-Spring Boot嵌入tomcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将最大文件大小设置为

I have set max file size to

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中处理异常.每次我最终都会得到相同的结果-服务器崩溃.

And final result after request with over-sized file is problem loading page. I dont get any other error in stack trace so i am kinda stuck with what is actually going on. Oh yeah i have tried many other solutions such as registering filter, handling exception in ErrorController. Every time i would end up with same result - server 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);
   }
}

推荐答案

这很棘手. Tomcat属性MaxSwallowSize导致了此问题.显然,它是在Tomcat的最新版本中引入的.它背后的全部想法是,如果Tomcat意识到要拒绝该请求,则终止高于默认2mb的任何连接(至少这是我的解释).简单覆盖此属性可以解决问题.我意识到这不是一个完美的解决方案,但是比终止连接要好得多.

This was tricky. Tomcat property MaxSwallowSize was causing this problem. Apparently it was introduced in one of the recent versions of Tomcat. The whole idea behind it was if Tomcat realized the request was going to be rejected, to terminate the connection anything higher than default 2mb (at least this was my interpretation). Simple overriding this property fixes things. I realize this is not perfect solution, but it is a whole lot better than just terminating connection.

@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;
}

这篇关于分段文件最大大小异常-Spring Boot嵌入tomcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆