tangxiaobao
2021-07-27 db9d95db6f25f95cbc614525adef7dbdeae6478c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.panzhihua.zuul.handles;
 
import com.panzhihua.common.constants.HttpStatus;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.utlis.ResultUtil;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@ControllerAdvice
public class FileUploadExceptionAdvice {
 
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void handleMaxSizeException(
            MaxUploadSizeExceededException exc,
            HttpServletRequest request,
            HttpServletResponse response) {
        //请求超过大小限制
        response.setContentType("application/json;charset=utf-8");
        ResultUtil.responseJson(response, R.fail(HttpStatus.REQUEST_TOO_LARGE, "请求主体的大小超过限制"));
        return;
 
    }
 
    @ExceptionHandler(FileSizeLimitExceededException.class)
    public void handleFileSizeLimitException(
            FileSizeLimitExceededException exc,
            HttpServletRequest request,
            HttpServletResponse response) {
        //请求超过大小限制
        response.setContentType("application/json;charset=utf-8");
        ResultUtil.responseJson(response, R.fail(HttpStatus.REQUEST_TOO_LARGE, "文件大小超过限制"));
        return;
    }
 
}