第一种方法是通过javascript校验来限制,这个比较简单,获取input的value然后截取扩展名进行判断即可
第二种是根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制,步骤如下:
maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位). 注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB
allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html),这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.
2 jsp页面定义如下(
testFileUpload.jsp
)
s:fielderror 显示i18n中配置的错误信息
<
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 upload extends ActionSupport {
// 封装上传文件域的属性
private File image;
// 封装上传文件类型的属性
private String imageContentType;
// 封装上传文件名的属性
private String imageFileName;
public String execute(){
String extName = ""; //保存文件拓展名
String newFileName = ""; //保存新的文件名
String nowTimeStr = ""; //保存当前时间
//生成随机文件名:当前年月日时分秒+五位随机数(为了在实际项目中防止文件同名而进行的处理)
int rannum = (int) (new Random().nextDouble() * (99999 - 10000 + 1)) + 10000; //获取随机数
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); //时间格式化的格式
nowTimeStr = sDateFormat.format(new Date()); //当前时间
//获取拓展名
if (imageFileName.lastIndexOf(".") >= 0){
extName = imageFileName.substring(imageFileName.lastIndexOf("."));
}
newFileName = nowTimeStr + rannum + extName; //文件重命名后的名字
image.renameTo(new File("E://"+ newFileName)); //保存文件
return null;
}
get/set......
}
注意:如果jsp中file的name="xxx",那么后台action中的属性要做相应更改为
private File xxx;
private String xxxContentType;
private String xxxFileName;
同时注意大小写一定要一致
struts.xml:
<struts>
<package name="uploadPackage" extends="struts-default">
<action name="uploadt" class="com.neau.project.Backstage.upload.upload">
<interceptor-ref name="fileUpload">
<param name="savePath">/jsp/</param>
<!-- 文件过滤 -->
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<!-- 文件大小, 以字节为单位 -->
<param name="maximumSize">1025956</param>
</interceptor-ref>
<!-- 默认拦截器必须放在fileUpload之后,否则无效 -->
<interceptor-ref name="defaultStack" />
<!-- 发生错误时返回的view -->
<result name="input">/jsp/admin/demo.jsp</result>
</action>
</package>
</struts>
4 定义错误文件类型的消息提示,这个需要用到struts2的资源文件,在struts.properties文件中加入
struts.custom.i18n.resources=globalMessages
globalMessages对应着资源文件名
5 在源文件夹下定义资源文件
globalMessages.properties,并在里面加入如下信息:
struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate
这里稍作说明(拷贝一下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
会给出提示:upload file contenttype is invalidate
原帖地址:http://www.blogjava.net/landor2004/archive/2009/06/11/281416.html