Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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 SizeLimitExceedeException未被弹簧控制器捕获?_Java_Spring - Fatal编程技术网

Java SizeLimitExceedeException未被弹簧控制器捕获?

Java SizeLimitExceedeException未被弹簧控制器捕获?,java,spring,Java,Spring,在使用Spring的Java web应用程序上,当我上载一个大于定义的最大大小的文件时,会抛出一个org.apache.commons.fileupload.FileUploadBase$SizeLimitExceedeException 我希望它能被我的自定义异常处理controller.class捕获,该类用@ControllerAdvice注释,并成功捕获我喜欢用特定逻辑处理的所有其他特定异常;但不是这个 是什么导致了这种问题行为,我如何修复它 例外情况很明显: Caused by: or

在使用Spring的Java web应用程序上,当我上载一个大于定义的最大大小的文件时,会抛出一个
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceedeException

我希望它能被我的自定义
异常处理controller.class
捕获,该类用
@ControllerAdvice
注释,并成功捕获我喜欢用特定逻辑处理的所有其他特定异常;但不是这个

是什么导致了这种问题行为,我如何修复它

例外情况很明显:

Caused by: org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (174470507) exceeds the configured maximum (7000000)
最大文件大小在my
applicationContext.xml
中定义如下:

<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="7000000" /><!-- amount is in Bytes -->
</bean>
web.xml
中:

<filter>
    <async-supported>true</async-supported>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

真的
多部件滤波器
org.springframework.web.multipart.support.MultipartFilter
多部件滤波器
/*
作为奖励,如果我可以在达到最大文件大小时抛出异常,而不是在整个上传结束后抛出异常,这将非常棒;但这应该是一个单独的问题:)

注意:我验证了该异常没有被其他处理程序捕获。它只是“绕过”一切

谢谢你的帮助!:)

==已验证答案的更新/反馈===

正如Elevate在验证答案中指出的,我只是从
web.xml
中删除了过滤器,并将bean配置从
applicationContext.xml
切换到
dispatcher servlet.xml
(纯拷贝粘贴)

另外,
异常处理控制器
现在捕捉到一个
MaxUploadSizeExceedeException
,而不是由于重新布线引起的
SizeLimitExceedeException

异常现在被正确捕获;谢谢你的帮助

你问:“什么会导致这种有问题的行为?”

多部分过滤器在控制器周围应用,因此控制器没有看到异常。它不会发生在控制器内部

for MultipartFilter表示,它是为不使用Spring的Web MVC的用例而设计的:

注意:此筛选器是使用DispatcherServlet的多部分解析器支持的替代方案,例如,对于具有自定义web视图的web应用程序(不使用Spring的web MVC),或者对于在Spring MVC DispatcherServlet之前应用的自定义筛选器

你会问,“我怎样才能解决这个问题?”

答案是(显然)将多部分处理放在控制器内。:)


您需要从
web.xml
中删除筛选器,并配置
CommonsMultipartResolver
在控制器范围内运行。我不知道你的控制器是如何配置的,所以你必须解决它,或者发布另一个问题

您可以通过以下方式编程创建解析器bean:

@Configuration
@Component
public class MultipartResolverBeanConfig {

  /**
   * Create the multipart resolver bean
   * Set the maximum upload file size as defined in the configuration file
   * @return the multipartResolver bean
   */
  @Bean(name="multipartResolver")
  public CommonsMultipartResolver multipartResolver() {
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setMaxUploadSize(100000);
    return multipartResolver;
  }
}
您可以将其更改为
maxUploadSizeExceedeException
,但也可以从中获得
SizeLimitExceedeException
,例如:

@ExceptionHandler(value = { MaxUploadSizeExceededException.class })
protected ResponseEntity<Object> handleMaxUploadSizeExceededException(Exception ex, WebRequest request) {
    MaxUploadSizeExceededException musee = (MaxUploadSizeExceededException)ex;
    SizeLimitExceededException slee = musee.getCause() instanceof SizeLimitExceededException ? (SizeLimitExceededException) musee.getCause() : null;
    Long maxSize = slee == null ? musee.getMaxUploadSize() : slee.getPermittedSize();
    Long actualSize = slee == null ? Long.parseLong(request.getHeader("Content-Length")) : slee.getActualSize();

    return handleExceptionInternal(ex, "error", new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE, request);
  }
@ExceptionHandler(值={MaxUploadSizeExceedeException.class})
protected ResponseEntity HandleMaxUploadSizeExceedeException(异常示例,WebRequest请求){
MaxUploadSizeExceedeException musee=(MaxUploadSizeExceedeException)ex;
SizeLimitExceedeException slee=musee.getCause()实例SizeLimitExceedeException?(SizeLimitExceedeException)musee.getCause():null;
Long maxSize=slee==null?musee.getMaxUploadSize():slee.getPermittedSize();
Long-actualSize=slee==null?Long.parseLong(request.getHeader(“内容长度”)):slee.getActualSize();
返回handleExceptionInternal(例如,“错误”,新的HttpHeaders(),HttpStatus.NOT_ACCEPTABLE,request);
}

更新了问题,并提供了如何回答问题的反馈
@ExceptionHandler(value = { MaxUploadSizeExceededException.class })
protected ResponseEntity<Object> handleMaxUploadSizeExceededException(Exception ex, WebRequest request) {
    MaxUploadSizeExceededException musee = (MaxUploadSizeExceededException)ex;
    SizeLimitExceededException slee = musee.getCause() instanceof SizeLimitExceededException ? (SizeLimitExceededException) musee.getCause() : null;
    Long maxSize = slee == null ? musee.getMaxUploadSize() : slee.getPermittedSize();
    Long actualSize = slee == null ? Long.parseLong(request.getHeader("Content-Length")) : slee.getActualSize();

    return handleExceptionInternal(ex, "error", new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE, request);
  }