org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException:

一:今天在使用struts2做文件上传时出现了该异常:

警告: Unable to parse request
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (5897994) exceeds the configured maximum (2097152)
	at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:937)
	at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
	at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
	at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parseRequest(JakartaMultiPartRequest.java:151)
	at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.processUpload(JakartaMultiPartRequest.java:90)
	at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:80)
	at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:75)
	at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:753)
	at org.apache.struts2.dispatcher.ng.PrepareOperations.wrapRequest(PrepareOperations.java:140)
	at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:83)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Unknown Source)
2014-7-31 22:53:04 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: the request was rejected because its size (5897994) exceeds the configured maximum (2097152)


为什么会出现这个错误?

查看struts.properties中有这么一句struts.multipart.parser=Jakarta,实际上这一句也可以不写因为这是struts2为了支持文件上传默认的。那么这个Jakarta到底是什么东西呢?实际上Jakarta实际上就是org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest类。打开JakartaMultiPartRequest.java的源代码你会发现这个类实现了MultiPartRequest接口,在这个类封装了对底层ServletFileUpload的操作,由于common-fileupload组件默认最大支持上传文件的大小为2M,当我们上传大于2M的文件时,就会出现上面的异常。是这个异常的发生导致了fileUpload拦截器没有机会执行,所以看到的是页面没有任何变化,也没有任何提示信息,只是在控制台打印出了上面的异常。解决的办法在struts.properties文件中把struts.multipart.maxSize设置成一个比较大的值,也是就说maxSize远远要大于可能上传文件的大小和fileUpload拦截器中maxinumSize的值。 如可以配置:struts.multipart.maxSize=10000000 即可。

默认的在struts中的default.properties中有这句话:struts.multipart.maxSize=2097152 将上传文件限制为2MB

我的配置是:在struts.xml中加入<constant name="struts.multipart.maxSize" value="524288000" />将默认的上传文件最大值变成500MB

Web容器启动的时候加载文件的时候先加载default.properties,然后再加载struts.xml配置文件。

你可能感兴趣的:(fileupload)