@ControllerAdvice注解的类无法捕获MaxUploadSizeExceededException文件上传大小限制异常解决办法

虽然知道请求的处理顺序如下:

avatar

全局异常处理类:

@RestControllerAdvice
public class MyGlobalExceptionHandler {
    
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Map handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 500);
        map.put("msg", "文件超出限制大小");
        return map;
    }

}

但是在springboot中使用注解@ControllerAdvice自定义了全局异常处理类,可就是捕获不了MaxUploadSizeExceededException异常;因为这个异常在ControllerAdvice这一层被抛出了,不知道什么原因不处理这个请求的异常;”有时候还真是国外的月亮圆“,众里寻他千百度,答案却在stackoverflow,以下是解决方案:

This is an old question so I'm adding it for the future people (including future me) who are struggling to get this working with Spring Boot 2.

At first you need to configure the spring application (in properties file):

spring.servlet.multipart.max-file-size=10MB				#设置单个文件的大小
spring.servlet.multipart.max-request-size=10MB			#设置单次请求的文件的总大小

If you're using embedded Tomcat (and most likely you are, as it comes as standard) it's also important to configure Tomcat not to cancel the request with large body

server.tomcat.max-swallow-size=-1

or at least set it to relatively big size

server.tomcat.max-swallow-size=100MB                   #请求正文的最大大小

If you will not set maxSwallowSize for Tomcat, you might waste lots of hours debugging why the error is handled but browser gets no response - that's because without this configuration Tomcat will cancel the request, and even though you'll see in the logs that application is handling the error, the browser already received cancellation of request from Tomcat and isn't listening for the response anymore.

And to handle the MaxUploadSizeExceededException you can add ControllerAdvice with ExceptionHandler.

Here's a quick example in Kotlin that simply sets a flash attribute with an error and redirects to some page:

@ControllerAdvice
class FileSizeExceptionAdvice {
    @ExceptionHandler(MaxUploadSizeExceededException::class)
    fun handleFileSizeException(
        e: MaxUploadSizeExceededException, 
        redirectAttributes: RedirectAttributes
    ): String {
        redirectAttributes.addFlashAttribute("error", "File is too big")
        return "redirect:/"
    }
}

NOTE: if you want to handle MaxUploadSizeExceededException with ExceptionHandler directly in your controller class, you should configure following property:

spring.servlet.multipart.resolve-lazily=true

otherwise that exception will be triggered before the request is mapped to controller.

出处:https://stackoverflow.com/questions/2689989/how-to-handle-maxuploadsizeexceededexception

因为Springboot内嵌tomcat也有文件大小限制,默认为:DataSize.ofMegabytes(2) = 2MB

avatar

附:

spring.servlet.multipart.max-file-size=10MB				#设置单个文件的大小
spring.servlet.multipart.max-request-size=10MB		                #设置单次请求的文件的总大小
server.tomcat.max-swallow-size=100MB                                    # 内置tomcat上传限制
#server.tomcat.max-swallow-size=-1                                       #注意这点,使用内置tomcat注意设置;设置-1不限制,或者设置个较大点的数
spring.servlet.multipart.resolve-lazily=true			        #推迟文件解析,以便捕获文件大小异常
posted @ 2020-05-29 04:38  youngyajun  阅读(991)  评论(0编辑  收藏  举报