mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.zuul.handles;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
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 com.panzhihua.common.constants.HttpStatus;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.utlis.ResultUtil;
 
@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;
    }
 
}