multipart.maxFileSize: 1mb
multipart.maxRequestSize: 1mb
这是我的控制器:
@RequestMapping(method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Secured(Privileges.CAN_USER_READ)
public void create(@RequestParam("file")final MultipartFile file,Principal principal) throws IllegalStateException, IOException,MultipartException{
medicalHistoryService.create(new MedicalHistory(file));
}
这是错误消息
2016-03-03 13:48:24.560 WARN 4992 --- [nio-8080-exec-1] h.c.w.RestResponseEntityExceptionHandler : Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (9288401) exceeds the configured maximum (1048576)
2016-03-03 13:48:25.545 WARN 4992 --- [nio-8080-exec-2] h.c.w.RestResponseEntityExceptionHandler : Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (9288401) exceeds the configured maximum (1048576)
请求过大的文件后的最终结果是页面加载问题。在堆栈跟踪中没有其他错误,所以我有点被卡住了,不知道实际发生了什么。噢,是的,我尝试了很多其他解决方案,比如注册过滤器,在ErrorController中处理异常。每次我最终都会得到同样的结果 - 服务器崩溃。
编辑 2
我的异常处理类:
@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{
// 413 MultipartException - file size too big
@ExceptionHandler({MultipartException.class,FileSizeLimitExceededException.class,java.lang.IllegalStateException.class})
public ResponseEntity<Object> handleSizeExceededException(final WebRequest request, final MultipartException ex) {
//log.warn("413 Status Code. File size too large {}", ex.getMessage());
log.warn(ex.getMessage());
final ApiError apiError = message(HttpStatus.PAYLOAD_TOO_LARGE, ex);
return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.PAYLOAD_TOO_LARGE, request);
}
}
handleExceptionInternal
是做什么的?提供一个最小化、完整化、可验证化的示例将使人们更容易地帮助你。 - Andy WilkinsonhandleExceptionInternal
是ResponseEntityExceptionHandler
的内部方法,根据文档说明,它是一个方便的基类,用于提供集中式异常处理的{@link ControllerAdvice @ControllerAdvice}类。 - SeaBiscuit