struts2文件上传大小限制问题小结(引用)

最后解决办法:

  1. 页面js控制上传文件的大小,在页面进行控制。如下代码 inputs是所有文本上传input DOM
    •     //名称信息
      var nameStr='';
      //大小信息
      var sizeStr='';
      inputs.each(function(i,n){
      //文件名路径
      var name =n.value;
      //获取文件名
      var index = name.lastIndexOf('\\');
      name = name.substring(index+1,name.length);
      //拼接文件名
      nameStr +=name+',';
      if(n.files[0]){
      var size=n.files[0].size;
      var sizeNum = size/1048576;
      if(sizeNum<10){
      sizeNum=sizeNum.toFixed(3);
      }else if(sizeNum<100){
      sizeNum=sizeNum.toFixed(2);
      }else{
      sizeNum=sizeNum.toFixed(1);
      }
      fileAry.push({'name':name,'size':sizeNum});
      sizeStr +=sizeNum+"M,";
      }
      });
      //去掉逗号
      nameStr=nameStr.substring(0,nameStr.length-1);
      //去掉逗号
      sizeStr=sizeStr.substring(0,sizeStr.length-1);

转自:http://www.cnblogs.com/godtrue/p/4311486.html

一:首先看一下程序执行中出现的对应报错信息,如下所示:

struts2文件上传大小限制问题小结(引用)
[WARN ] 2015-03-03 15:31:11 :Unable to parse request
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (78871114) exceeds the configured maximum (52428800)
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:740)
at org.apache.struts2.dispatcher.ng.PrepareOperations.wrapRequest(PrepareOperations.java:131)
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 com.frame.action.ChineseFilter.doFilter(ChineseFilter.java:66)
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:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
[WARN ] 2015-03-03 15:31:11 :Could not find token name in params.
struts2文件上传大小限制问题小结(引用)

类似的问题相信很多人都遇到过,也晓得如何解决,我这里记录下来也是为了让自己对这个问题理解的更好一点,当然,我下面记录的情况都是在我的机器上实际都试验了的,下面是我根据自己的实验以及网上的相关资料的小结。

二:限制Struts2文件上传的三种情况

1:Struts2对应的配置文件中的配置的限制

struts.multipart.maxSize=上传文件的最大值(单位是字节)这个配置是控制整个项目一次性上传的文件的最大值!如果实际上传的文件超过了此配置的大小那么就会报  org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (78871114) exceeds the configured maximum (52428800),这个异常信息!

这个配置有对应的默认值,如果不设置则使用默认的设置(针对我自己的项目这个默认配置的所在位置是:struts2-core-2.3.1.2.jar包中的org\apache\struts2\default.properties文件中)struts.multipart.maxSize=2097152,代表默认上传的文件最大值是2M,如果我们想修改它也是很容易的,有以下两种方式:

1)在struts.properties文件中配置,比如:struts.multipart.maxSize=52428800 (代表上传的文件的最大值是50M)

2)在struts.xml文件中配置,比如:<constant name="struts.multipart.maxSize" value="52428800"/>(也代表上传的文件的最大值是50M)

2:Struts2对应的文件上传的拦截器的限制

        <interceptor-ref name="fileUpload">
<param name="maximumSize">52428800</param>
</interceptor-ref>

这种方式是限制特定的Action的一种方式,当然她是在满足第一种情况下才起作用的,换言之,这里的配置要小于第一种情况的设置才能起到对应的作用!

需要注意的是,拦截器的使用以及和第一种文件上传之间的配合,情况如下(下面的代码仅仅是事例代码而已):

2-1:没有拦截器的情况,仅第一种限制起作用

        <action name="fileUpload" class="fileUploadAction" method="fileUpload">
</action>

2-2:仅使用默认的拦截器的情况,和2-1的效果一样,它们两个的本质是一样的,因为Struts2会每个Action加入一个默认的拦截器,显示说明与否皆可!

        <action name="fileUpload" class="fileUploadAction" method="fileUpload">
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

2-3:仅使用文件上传的拦截器的情况,第一种限制起作用,第二种限制也有效果,不过这种使用拦截器的方式是有问题的,这里仅仅使用了文件上传的拦截器而已,导致数据信息的丢失!当实际上传的文件大于拦截器的配置的时候,会报([WARN ] 2015-03-03 18:49:44 :File too large: fileUploadModel.upload "test.png" "upload_6e4dba7d_14be260bb68__7fe3_00000018.tmp" 11185361)不过程序会继续的执行!

        <action name="fileUpload" class="fileUploadAction" method="fileUpload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">10485760</param>
</interceptor-ref>
</action>

2-4:先使用文件上传的拦截器,再使用默认的拦截器的情况,第一种限制起作用,第二种限制也起作用,并且当实际上传的文件的大小超过拦截器的限制时,程序会报错([WARN ] 2015-03-03 18:53:30 :File too large: fileUploadModel.upload "test.png" "upload_53477faa_14bdea02616__7fef_00000008.tmp" 11185361)且程序会终止执行(调试的结果显示没有进入相应的Action类中)!

        <action name="fileUpload" class="fileUploadAction" method="fileUpload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">10485760</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>

2-5:先使用默认的拦截器,再使用文件上传的拦截器的情况,第一种限制起作用,第二种限制也起作用,不过当超过拦截器的限制时,程序会报错([WARN ] 2015-03-03 18:58:30 :File too large: fileUploadModel.upload "test.png" "upload_53477faa_14bdea02616__7fef_00000008.tmp" 11185361)不过仍继续的执行!

        <action name="fileUpload" class="fileUploadAction" method="fileUpload">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="fileUpload">
<param name="maximumSize">10485760</param>
</interceptor-ref>
</action>

 3:上传文件的最大限制

文件上传的最大值应<2G,无论前面两项配置的是多大,当实际上传的文件大小大于2G的时候就会出现如下图的情况:

struts2文件上传大小限制问题小结(引用)

针对这个情况,我还不明白是什么原因引起的,如果有清楚的请告知一下,非常感谢!

三:对应的源码的情况

第一种限制对应的关键性源码(位置:commons-fileupload-1.2.2.jar包下\org\apache\commons\fileupload\FileUploadBase.java)

struts2文件上传大小限制问题小结(引用)
FileItemIteratorImpl(RequestContext ctx)
throws FileUploadException, IOException
{
if (ctx == null) {
throw new NullPointerException("ctx parameter");
} String contentType = ctx.getContentType();
if ((null == contentType) || (!contentType.toLowerCase().startsWith("multipart/")))
{
throw new FileUploadBase.InvalidContentTypeException("the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is " + contentType);
} InputStream input = ctx.getInputStream(); if (FileUploadBase.this.sizeMax >= 0L) {
int requestSize = ctx.getContentLength();
if (requestSize == -1)
input = new LimitedInputStream(input, FileUploadBase.this.sizeMax, FileUploadBase.this) { private final FileUploadBase val$this$0; protected void raiseError(long pSizeMax, long pCount) throws IOException { FileUploadException ex = new FileUploadBase.SizeLimitExceededException("the request was rejected because its size (" + pCount + ") exceeds the configured maximum" + " (" + pSizeMax + ")", pCount, pSizeMax); throw new FileUploadBase.FileUploadIOException(ex);
}
};
//看这里
else if ((FileUploadBase.this.sizeMax >= 0L) && (requestSize > FileUploadBase.this.sizeMax)) {
throw new FileUploadBase.SizeLimitExceededException("the request was rejected because its size (" + requestSize + ") exceeds the configured maximum (" + FileUploadBase.this.sizeMax + ")", requestSize, FileUploadBase.this.sizeMax);
} } String charEncoding = FileUploadBase.this.headerEncoding;
if (charEncoding == null) {
charEncoding = ctx.getCharacterEncoding();
} this.boundary = FileUploadBase.this.getBoundary(contentType);
if (this.boundary == null) {
throw new FileUploadException("the request was rejected because no multipart boundary was found");
} this.notifier = new MultipartStream.ProgressNotifier(FileUploadBase.this.listener, ctx.getContentLength()); this.multi = new MultipartStream(input, this.boundary, this.notifier);
this.multi.setHeaderEncoding(charEncoding); this.skipPreamble = true;
findNextItem();
}
struts2文件上传大小限制问题小结(引用)

第二种限制对应的关键性源码(位置:struts2-core-2.3.1.2.jar包下\org\apache\struts2\interceptor\FileUploadInterceptor.java)

struts2文件上传大小限制问题小结(引用)
  protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation, Locale locale)
{
boolean fileIsAcceptable = false; if (file == null) {
String errMsg = getTextMessage(action, "struts.messages.error.uploading", new Object[] { inputName }, locale);
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled())
LOG.warn(errMsg, new String[0]);
}
//看这里
else if ((this.maximumSize != null) && (this.maximumSize.longValue() < file.length())) {
String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new Object[] { inputName, filename, file.getName(), "" + file.length() }, locale);
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled())
LOG.warn(errMsg, new String[0]);
}
else if ((!this.allowedTypesSet.isEmpty()) && (!containsItem(this.allowedTypesSet, contentType))) {
String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new Object[] { inputName, filename, file.getName(), contentType }, locale);
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled())
LOG.warn(errMsg, new String[0]);
}
else if ((!this.allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(this.allowedExtensionsSet, filename))) {
String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed", new Object[] { inputName, filename, file.getName(), contentType }, locale);
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled())
LOG.warn(errMsg, new String[0]);
}
else {
fileIsAcceptable = true;
} return fileIsAcceptable;
}
struts2文件上传大小限制问题小结(引用)

 四:参考的网络资源如下

1)Struts2文件上传大小限制相关

http://www.cnblogs.com/highriver/archive/2011/06/01/2065557.html

http://www.cnblogs.com/forlina/archive/2011/09/08/2171404.html

http://www.docin.com/p-633913742.html

2)Struts2拦截器相关

http://struts2.group.iteye.com/group/wiki/1397-deep-into-struts2-interceptors

http://www.open-open.com/lib/view/open1338339244354.html

http://blog.****.net/qjyong/article/details/1824607

上一篇:Struts2 文件上传


下一篇:php页面之间传值