springboot 上传图片超过限制抛异常 FileSizeLimitExceededException

Source

MultipartException:Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field editormd-image-file exceeds its maximum permitted size of

解决方案:对 controller 进行 MultipartException 的异常捕捉。添加如下代码即可。如果不是 rest 风格或者返回的不是 json,需要进行修改。

@RestControllerAdvice
public class JsonExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(JsonExceptionHandler.class);

    // 图片大小超过指定大小,抛异常
    @ResponseBody
    @ExceptionHandler(value = MultipartException.class)
    public Map<String, Object> resolveFileUploadException(MultipartException e) {
        logger.info("MultipartException:"+e.getMessage());
        Map<String, Object> map = new HashMap<>();
        map.put("success", 0);
        map.put("message", "上传错误:图片大小超过指定值。建议10M以内");
        return map;
    }
}

参考:http://www.fengyunxiao.cn