1.UploadAction.java
package lee;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
//接爱依赖注入的属性
private String savePath;
private String allowTypes;
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
//接受依赖注入的方法
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getAllowTypes() {
return allowTypes;
}
public void setAllowTypes(String allowTypes) {
this.allowTypes = allowTypes;
}
//过滤方法
public String filterType(String[] types){
String fileType=this.getUploadContentType();
for(String type:types){
if(type.equals(fileType)){
return null;
}
}
return INPUT;
}
public String execute()throws Exception{
String filterResult=filterType(this.getAllowTypes().split(","));
if(filterResult!=null){
ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
return filterResult;
}
System.out.println("文件大小---------------"+this.getUpload().length());
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
FileInputStream fis=new FileInputStream(this.getUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}
2.struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="globalMessages"/>
<constant name="struts.i18n.encoding" value="GBK"/>
<package name="upload" extends="struts-default">
<action name="upload" class="lee.UploadAction">
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="savePath">/upload</param>
<result name="input">/upload.jsp</result>
<result>/succ.jsp</result>
</action>
</package>
</struts>
3.upload.jsp
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>利用拦截器实现文件过滤</title>
</head>
<body>
${requestScope.typeError}
<form action="upload.action" method="post" enctype="multipart/form-data">
文件标题:<input type="text" name="title" /><br>
选择文件:<input type="file" name="upload" /><br>
<input value="上传" type="submit" />
</form>
</body>
</html>
4.succ.jsp
<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传成功!</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value="+ title"/>
文件为:<img src="<s:property value="'upload/' + uploadFileName"/>"/><br>
</body>
</html>