MaxUploadSizeExceededException当我上传大小超过允许的最大值的文件时,会出现异常.我想在出现此异常时显示错误消息(如验证错误消息).如何在Spring 3中处理此异常以执行此类操作?
谢谢.
我正在使用Spring 3.2.0.根据这个答案,我在带注释的控制器中使用相同的方法来实现HandlerExceptionResolver接口,例如:
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {
Map<String, Object> model = new HashMap<String, Object>(0);
if (exception instanceof MaxUploadSizeExceededException) {
model.put("msg", exception.toString());
model.put("status", "-1");
} else {
model.put("msg", "Unexpected error : " + exception.toString());
model.put("status", "-1");
}
return new ModelAndView("admin_side/ProductImage");
}
Run Code Online (Sandbox Code Playgroud)
和Spring配置包括,
<bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>10000</value>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
当文件大小超过时,应该调用前面的方法,它应该自动处理异常,但根本不会发生.resolveException()即使发生异常,也不会调用该方法.处理此异常的方法是什么?我错过了什么吗?
这里也指出了同样的事情.我不确定为什么它在我的情况下不起作用.
我尝试了以下方法,@ControllerAdvice但它也没有用.
package exceptionhandler;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import …Run Code Online (Sandbox Code Playgroud) spring exception-handling file-upload spring-mvc handlerexceptionresolver