开始只在struts.xml文件中加入以下配置
<constant name="struts.multipart.maxSize" value="10000" />
在index.jsp文件中加入的 <s:fielderror/>没有显示
只会在控制台打印错误信息:
警告: Request exceeded size limit!
org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (251842) exceeds the configured maximum (10000)
解决方法:
<constant name="struts.multipart.maxSize" value="100000000" />
在拦截器中添加以下配置
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/png,image/jpeg</param>
<param name="maximumSize">10000</param> <!--此处的值应小于常量中设置的最大值-->
</interceptor-ref>
<!-- 默认的拦截器栈 -->
<interceptor-ref name="defaultStack"/>
结果如下:
将<param name="maximumSize">10000</param>中的值设置大一些则该大小文件能正常上传。
说明:根据struts源码/org/apache/struts2/struts-messages.properties
该文件中的此段可得上述结论
struts.messages.bypass.request=Bypassing {0}/{1}
struts.messages.current.file=File {0} {1} {2} {3}
struts.messages.invalid.file=Could not find a Filename for {0}. Verify that a valid file was submitted.
struts.messages.invalid.content.type=Could not find a Content-Type for {0}. Verify that a valid file was submitted.
struts.messages.removing.file=Removing file {0} {1}
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File {0} is too large to be uploaded. Maximum allowed size is {4} bytes!
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}