关于spring mvc MaxUploadSizeExceededException 死循环解决方案

王朝学院·作者佚名  2016-05-28
窄屏简体版  字體: 小  |  中  |  大  |  超大  

关于sPRing mvc MaxUploadSizeExceededException 死循环解决方案当看到这文章的时候相信你现在应该遇到这样的问题了,我也是自己遇到了后来找到解决方案了记录下来,如果下次遇到就可以直接解决了。

至于为什么会出现这样的情况,可以看这篇文章:https://bz.apache.org/bugzilla/show_bug.cgi?id=57438

这些一个tomcat bug,但不知道什么版本解决了这个问题。所以下面用了另外一个方法搞定它。

<!-- 多部分文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 最大上传大小,不能超过40M @see #FileUploadInterceptor --> <property name="maxUploadSize" value="41943040"/> <property name="maxInMemorySize" value="4096"/> <property name="defaultEncoding" value="UTF-8"/> </bean>

首先,把maxUploadSize 设置大一点,让它不会抛异常出来。

然后创建一个拦截器控制上传大小,然后抛出同样的异常出来,或者也可以直接在拦截器中处理异常

public class FileUploadInterceptor implements HandlerInterceptor { private long maxSize; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if(request!=null && ServletFileUpload.isMultipartContent(request)) { ServletRequestContext ctx = new ServletRequestContext(request); long requestSize = ctx.contentLength(); if (requestSize > maxSize) { throw new MaxUploadSizeExceededException(maxSize); } } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } public void setMaxSize(long maxSize) { this.maxSize = maxSize; }}

然后在xml中配置

<mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.lvrounet.peiniang.interceptor.FileUploadInterceptor"> <property name="maxSize" value="4194304"/> </bean> </mvc:interceptor>

拦截器中可以根据需求配置上传的大小了。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航