Multipart File Upload:Size exceed exception in spring boot return JSON error message

前端 未结 2 654
悲&欢浪女
悲&欢浪女 2021-02-09 03:00

As I have set maximum file upload limit,I am getting

org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceed         


        
相关标签:
2条回答
  • 2021-02-09 03:26

    As par I know you can handle the multipart file exception by using this.

    @ControllerAdvice
    public class MyErrorController extends ResponseEntityExceptionHandler {
    
    Logger logger = org.slf4j.LoggerFactory.getLogger(getClass());
    
    @ExceptionHandler(MultipartException.class)
    @ResponseBody
    String handleFileException(HttpServletRequest request, Throwable ex) {
        //return your json insted this string.
        return "File upload error";
      }
    }
    
    0 讨论(0)
  • 2021-02-09 03:34

    Add a special exception handler into your Controller:

    @ExceptionHandler(FileSizeLimitExceededException.class)
    public YourReturnType uploadedAFileTooLarge(FileSizeLimitExceededException e) {
        /*...*/
    }
    

    (If this does not work, you have to enable exception handling in your configuration. Normally Spring does this by default.)

    0 讨论(0)
提交回复
热议问题