struts2文件上传中,如何限制上传的文件类型

struts2版本是2.1.6
struts2是根据contentType来限制的,并不是文件的扩展名
比如我想仅上传image/png,image/gif,image/jpeg这三种文件类型

第一种方法是通过javascript校验来限制,这个比较简单,获取input的value然后截取扩展名进行判断即可

第二种是根据 struts2自带的fileupload拦截器中提供的allowedTypes来进行限制,步骤如下:
1 配置fileupload拦截器
struts2的defaultStack中已经含有fileupload拦截器,如果想加入allowedTypes参数,需要从新写一个defaultstack ,拷贝过来修改一下即可:
< interceptor-stack name ="myDefaultStack" >
< interceptor-ref name ="exception" />
< interceptor-ref name ="alias" />
< interceptor-ref name ="servletConfig" />
< interceptor-ref name ="i18n" />
< interceptor-ref name ="prepare" />
< interceptor-ref name ="chain" />
< interceptor-ref name ="debugging" />
< interceptor-ref name ="profiling" />
< interceptor-ref name ="scopedModelDriven" />
< interceptor-ref name ="modelDriven" />
< interceptor-ref name ="fileUpload" >
< param name ="allowedTypes" >
image/png,image/gif,image/jpeg
</ param >
</ interceptor-ref >
< interceptor-ref name ="checkbox" />
< interceptor-ref name ="staticParams" />
< interceptor-ref name ="actionMappingParams" />
< interceptor-ref name ="params" >
< param name ="excludeParams" > dojo\..*,^struts\..* </ param >
</ interceptor-ref >
< interceptor-ref name ="conversionError" />
< interceptor-ref name ="validation" >
< param name ="excludeMethods" > input,back,cancel,browse </ param >
</ interceptor-ref >
< interceptor-ref name ="workflow" >
< param name ="excludeMethods" > input,back,cancel,browse </ param >
</ interceptor-ref >
</ interceptor-stack >
</ interceptors >
< default-interceptor-ref name ="myDefaultStack" ></ default-interceptor-ref >
仅修改代码中的
< interceptor-ref name ="fileUpload" >
< param name ="allowedTypes" >
image/png,image/gif,image/jpeg
</ param >
</ interceptor-ref >
上面配置的是上传文件类型的限制,其实共有两个参数
maximumSize(可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位).注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB
allowedTypes(可选)
- 以逗号分割的contentType类型列表(例如text / html),这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.


2 jsp页面定义如下( testFileUpload.jsp
< s:form action ="testFileUpload" method ="post" enctype ="multipart/form-data" >
< s:file name ="file" theme ="simple" />
< s:fielderror name ="file" ></ s:fielderror >
< s:submit />
</ s:form >
3 后台的action声明如下(我用的是 struts2的注解进行action配置)
public class TestFileUploadAction extends ActionSupport{
private Filefile;
private StringfileContentType;
private StringfileFileName;
@Action(
value
= " testFileUpload " ,results = {
@Result(name
= " input " ,location = " /testFileUpload.jsp " ),
@Result(name
= " success " ,location = " /testFileUploadSuccess.jsp " )
}
)
public Stringexecute(){
return SUCCESS;
}
get/set......
}
注意:如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为
private File xxx ;
private String xxx ContentType;
private String xxx FileName;
同时注意大小写一定要一致

4 定义错误文件类型的消息提示,这个需要用到 struts2的资源文件,在struts.properties文件中加入
struts.custom.i18n.resources = globalMessages
globalMessages对应着资源文件名

5 在源文件夹下定义资源文件
globalMessages.properties,并在里面加入如下信息:
struts.messages.error.content.type.not.allowed = uploadfilecontenttypeisinvalidate

这里稍作说明(拷贝一下struts2的帮助):
如果你的action实现了ValidationAware接口(如果action继承了ActionSupport,那么就相当于实现了ValidationAware),这个拦截器就可以添加几种字段错误.这些错误信息是基于存储在struts-messages.properties文件中的一些i18n值,这个文件是所有i18n请求的默认文件.你可以在自己消息文件的复写以下key的消息文字
struts.messages.error.uploading - 文件不能上传的通用错误信息
struts.messages.error.file.too.large
- 上传文件长度过大的错误信息
struts.messages.error.content.type.not.allowed
- 当上传文件不符合指定的contentType


以上配置完毕后,测试一下,对于非法的contentType,例如xxx.log这个文件的的contentType是pplication/octet-stream
会给出提示: uploadfilecontenttypeisinvalidate
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值