struts2的文件上传(3):利用拦截器实现文件过滤

手动实现文件过滤的思路简单,但是需要编写大量的过滤代码,不利于程序的高层次解构,而且开发复杂。

Struts2提供了一个文件上传的拦截器fileUpload,通过配置该拦截器可以更轻松地实现文件过滤。

配置fileUpload拦截器时,可以为其指定两个参数:

① allowedTypes : 该参数指定允许上传的文件类型,多个文件类型之间以英文逗号隔开。

② maximumSize : 该参数指定允许上传的文件大小,单位是字节。


fileupload.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<s:form action="upload" enctype="multipart/form-data" method="post">
<s:file name="file" label="上传文件"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
struts.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    <constant name="struts.custom.i18n.resources" value="mineI18N"></constant>
    
    <package name="demo" extends="struts-default">
        <action name="upload" class="action.UploadAction">
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/png,image/gif,image/jpeg</param>
                <param name="maximumSize">10000</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <param name="savePosition">/uploadDir</param>
            <result name="success">/success.jsp</result>
            <result name="input">/fileupload.jsp</result>
        </action>
    </package>
</struts>
UploadAction.java :

public class UploadAction extends ActionSupport {
	
	private File file;
	private String fileContentType;
	private String fileFileName;
	private String savePosition;
	
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getSavePosition() {
		return savePosition;
	}
	public void setSavePosition(String savePosition) {
		this.savePosition = savePosition;
	}
	@Override
	public String execute() throws Exception {
		String realPath=
			ServletActionContext.getServletContext().getRealPath(getSavePosition());
		FileOutputStream fos=new FileOutputStream(
				realPath+File.separator+getFileFileName());
		FileInputStream fis=new FileInputStream(getFile());
		byte[] buffer=new byte[1024];
		int len=0;
		while((len=fis.read(buffer))!=-1){
			fos.write(buffer,0,len);
		}
		return "success";
	}
}
success.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<s:property value="fileFileName"/>上传成功...
</body>
</html>
mineI18N_zh_CN.properties :

struts.messages.error.content.type.not.allowed=\u53ea\u5141\u8bb8\u4e0a\u4f20\u56fe\u7247\u7c7b\u578b\u6587\u4ef6
struts.messages.error.file.too.large=\u4f60\u4e0a\u4f20\u7684\u6587\u4ef6\u592a\u5927\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9
如果上传失败,系统返回input逻辑视图,也就是fileupload.jsp文件,Struts2可以自动显示上传失败的提示信息,并让浏览者重新上传。但系统默认的上传失败提示信息是英文,对于中文用户,不十分友好。我们可以使用国际化信息替换它。

在struts.xml 中加上如下配置:

 <constant name="struts.custom.i18n.resources" value="mineI18N"></constant>
在src目录下加上mineI18N_zh_CN.properties文件。上传文件太大的提示信息的key是“ struts.messages.error.file.too.large“,上传文件的类型不符合要求的提示信息的key是” struts.messages.error.content.type.not.allowed“。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值