Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring多部分文件上载大小_Java_Spring_Spring Mvc_Spring Boot_Multipart - Fatal编程技术网

Java Spring多部分文件上载大小

Java Spring多部分文件上载大小,java,spring,spring-mvc,spring-boot,multipart,Java,Spring,Spring Mvc,Spring Boot,Multipart,我的应用程序设置了以下属性 spring.http.multipart.max-file-size = 10MB 及 但不是抛出下面的Springs默认异常 { "message": "Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLi

我的应用程序设置了以下属性

spring.http.multipart.max-file-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" }
我想抛出
CustomException
,并显示类似
FILE\u SIZE\u TOO\u BIG
的消息。这可能吗?

我可以使用
@ControllerAdvice
吗?

在Spring应用程序中有很多方法来处理异常。参见主题。我认为从提供的文章中获取的
GlobalControllerExceptionHandler
将符合您的要求。请尝试此处理程序:

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
    @ExceptionHandler(MultipartException.class)
    public void handleMultipart(MultipartException exception) {
        // to do
    }
}

如果您通过组件扫描获取该类,它应该处理从任何控制器抛出的
多端口异常。

为什么要使用
@ControllerAdvice
?我想用自定义消息抛出CustomException。我同意任何其他建议。也许我对@ControllerAdvice的功能感到困惑。为什么您不能在上载代码块中捕获异常并将自定义异常向上游抛出?由于有效负载太大,不需要多端口异常。对于这种情况,有一个特定的扩展了MultipartException的
MaxUploadSizeExcepedException
@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(value = HttpStatus.PAYLOAD_TOO_LARGE)
    @ExceptionHandler(MultipartException.class)
    public void handleMultipart(MultipartException exception) {
        // to do
    }
}