在使用Struts2的文件上传时,文件大小和类型过滤后,页面无法跳转至input页面
-- 2010/07/25
问题描述:
在使用Struts2的文件上传时,文件大小和类型过滤后,页面无法跳转至input页面
struts.xml配置文件如下:
原因是com.dream.actions.FileUploadAction未继承ActionSupport
导致拦截器的跳转到input页面的功能不能真正使用
文件上传的过滤先后顺序是:
1.先判断表单提交的数据大小,是否超过配置文件“struts.multipart.maxSize”的值(默认大小:2097152 B);
如果此处不满足,则会报出下面信息:
警告: Unable to parse request
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (4652587) exceeds the configured maximum (2097152)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:914)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
at org.apache.struts2.dispatcher.multipart.JakartaMultiPartRequest.parse(JakartaMultiPartRequest.java:93)
at org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper.<init>(MultiPartRequestWrapper.java:75)
at org.apache.struts2.dispatcher.Dispatcher.wrapRequest(Dispatcher.java:708)
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 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: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:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
2010-7-25 19:20:51 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: the request was rejected because its size (4652587) exceeds the configured maximum (2097152)
2.再判断上传的文件数据是否超过拦截器里配置的“maximumSize”的大小;
如果此处不满足,则会报出下面信息:
警告: File too large: upload "DSC00034.JPG" "upload_38f8bfe7_12a07e9fd3d__7ff2_00000001.tmp" 4652362
2010-7-25 20:23:59 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Removing file upload D:/Java/Tomcat6/work/Catalina/localhost/Struts2BaseDemo/upload_38f8bfe7_12a07e9fd3d__7ff2_00000001.tmp
3.最后检查是否文件类型是否满足拦截器的条件;
如果此处不满足,则会报出下面信息:
警告: 您上传的文件不是图片类型!
2010-7-25 18:19:54 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Removing file upload D:/Java/Tomcat6/work/Catalina/localhost/Struts2BaseDemo/upload_38f8bfe7_12a07e9fd3d__7ff3_00000000.tmp
另外,也有的人喜欢这样来配置文件上传拦截器:
因为默认拦截器栈“defaultStack”是包含文件上传拦截器的,所以依拦截的先后顺序,此处文件上传拦截器会执行2次,建议使用第一种配置方式,直接引用默认拦截器栈“defaultStack”,再覆盖里面的“fileUpload”拦截器属性即可。