Struts2文件上传(二)-文件上传拦截器

看官方文档学习了一下,稍微记录一下。

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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值