大部分时候,Web应用不允许浏览者自由上传文件,尤其不能允许上传可执行性文件------因为可能是病毒程序。通常,我们可以允许浏览者上传图片,压缩文件等;除此之外,还必须对浏览者上传的文件大小进行设置。因此必须在文件上传中进行文件过滤。本篇文章只讨论过滤文件类型。
从处理表单文件域的Action中可以看出,它里面有两个方法分别用于获取文件类型和大小。为了实现文件过滤,可以通过判断这两个方法的返回值来实现文件过滤。
手动实现文件过滤,可以按照如下步骤进行:
① 在struts.xml中为Action配置一个allowedTypes参数,类型之间以逗号分隔。
<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
② 在UploadAction.java中添加一个String类型的allowedTypes属性,并加上对应的setter和getter方法。
private String allowedTypes;
public String getAllowedTypes() {
return allowedTypes;
}
public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}
③ 在UploadAction.java中定义一个专门用于文件过滤的方法,方法名自定。该方法的逻辑就是判断上传文件的类型是否为允许类型。如果为允许类型,返回null;如果为不允许的类型,返回一个字符串。
public String filterTypes(String[] types){
String realType=getFileContentType();
for(String type:types){
if(type.equals(realType)){
return null;
}
}
return ERROR;
}
④ 利用Struts2的输入校验来判断用户输入的文件类型是否符合要求,如果不符合要求,就将错误提示添加到FieldError中。
public void validate(){
String filterResult=filterTypes(getAllowedTypes().split(","));
if(filterResult!=null){
this.addFieldError("file","你上传的文件类型不被允许");
}
}
⑤ 因为文件类型校验失败时会自动返回input逻辑视图,所以还需要在struts.xml中为Action添加一个input逻辑视图所对应的物理视图。
<result name="input">/fileupload.jsp</result>
下面贴出完整的代码:
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>
<package name="demo" extends="struts-default">
<action name="upload" class="action.UploadAction">
<param name="savePosition">/uploadDir</param>
<param name="allowedTypes">image/png,image/gif,image/jpeg</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;
private String allowedTypes;
public File getFile() {
return file;
}
public void setFile(File file) {
System.out.println("setFile()被调用");
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
System.out.println("setFileContentType()被调用");
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
System.out.println("setFileFileName()被调用");
this.fileFileName = fileFileName;
}
public String getSavePosition() {
return savePosition;
}
public void setSavePosition(String savePosition) {
System.out.println("setSavePosition()被调用");
this.savePosition = savePosition;
}
public String getAllowedTypes() {
return allowedTypes;
}
public void setAllowedTypes(String allowedTypes) {
this.allowedTypes = allowedTypes;
}
@Override
public String execute() throws Exception {
System.out.println(getFileContentType());
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";
}
public String filterTypes(String[] types){
String realType=getFileContentType();
for(String type:types){
if(type.equals(realType)){
return null;
}
}
return ERROR;
}
public void validate(){
String filterResult=filterTypes(getAllowedTypes().split(","));
if(filterResult!=null){
this.addFieldError("file","你上传的文件类型不被允许");
}
}
}
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>