Spring 分段文件上传大小
Spring multipart file upload size
我的应用程序设置了以下属性
spring.http.multipart.max-file-size = 10MB
和
spring.http.multipart.max-request-size = 10MB
但不是在下面抛出 Springs 默认异常
{ "message": "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 (12780451) exceeds the configured maximum (10485760)", "type": "MultipartException" }
我想用 FILE_SIZE_TOO_BIG
这样的消息抛出 CustomException
。这可能吗?
我可以为此使用 @ControllerAdvice
吗?
在Spring应用程序中有很多处理异常的方法。请参阅 exception handling in Spring MVC 主题。我认为摘自所提供文章的 GlobalControllerExceptionHandler
将符合您的要求。试试这个处理程序:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
@ExceptionHandler(MultipartException.class)
public void handleMultipart(MultipartException exception) {
// to do
}
}
如果您通过组件扫描获取此 class,它应该处理从任何控制器抛出的 MultipartExceptions
。
我的应用程序设置了以下属性
spring.http.multipart.max-file-size = 10MB
和
spring.http.multipart.max-request-size = 10MB
但不是在下面抛出 Springs 默认异常
{ "message": "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 (12780451) exceeds the configured maximum (10485760)", "type": "MultipartException" }
我想用 FILE_SIZE_TOO_BIG
这样的消息抛出 CustomException
。这可能吗?
我可以为此使用 @ControllerAdvice
吗?
在Spring应用程序中有很多处理异常的方法。请参阅 exception handling in Spring MVC 主题。我认为摘自所提供文章的 GlobalControllerExceptionHandler
将符合您的要求。试试这个处理程序:
@ControllerAdvice
class GlobalControllerExceptionHandler {
@ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
@ExceptionHandler(MultipartException.class)
public void handleMultipart(MultipartException exception) {
// to do
}
}
如果您通过组件扫描获取此 class,它应该处理从任何控制器抛出的 MultipartExceptions
。