看官方文档学习了一下,稍微记录一下。
File Upload Interceptor
1.该拦截器有三个可选参数
- maximumSize:文件的最大字节数,默认是2M。注意,它和 struts.multipart.maxSize 是有区别的
文档上的描述:
There are two separate file size limits. First is struts.multipart.maxSize which comes from the Struts 2 default.properties file. This setting exists for security reasons to prohibit a malicious user from uploading extremely large files to file up your servers disk space. This setting defaults to approximately 2 megabytes and should be adjusted to the maximum size file (2 gigs max) that your will need the framework to receive. If you are uploading more than one file on a form the struts.multipart.maxSize applies to the combined total, not the individual file sizes. The other setting, maximumSize, is an interceptor setting that is used to ensure a particular Action does not receive a file that is too large.
- allowedTypes:允许上传的文件类型,如 text/html ,text/plain
- allowedExtensions:允许上传的文件的后缀,如 .html
如果文件不符合设置,会产生相应的错误。
2.错误类型
- struts.messages.error.uploading - a general error that occurs when the file could not be uploaded
- struts.messages.error.file.too.large - occurs when the uploaded file is too large
- struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expectedcontent types specified
- struts.messages.error.file.extension.not.allowed - occurs when the uploaded file does not match the expectedfile extensions specified
3.配置文件
<action name="upload" class="org.ygy.action.DocumentAction" method="upload">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg</param>
<param name="maximumSize">10240</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="input">/upload.jsp</result>//出错后,返回界面
<result name="success">/upload_success.jsp</result>
</action>