您的位置:首页 > 编程语言 > Java开发

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

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

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“。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: