1.FileUpload.action源代码
package com.file.application.action.upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpload extends ActionSupport {
private static final long serialVersionUID = 572146812454l;
private static final int BUFFER_SIZE = 16 * 1024;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String saveFileName;
private String savePath;
private static final String[] allowType = { ".xls", ".xlsx" };
/**
* excute方法
*/
@Override
public String execute() throws Exception {
// 修改上传后的文件名,避免重复上传
saveFileName = new Date().getTime()
+ this.getExtendsion(uploadFileName);
System.out.println("saveFilename=" + saveFileName);
// 以服务器保存文件+原文件名创建上传文件流
File saveFile = new File(ServletActionContext.getServletContext()
.getRealPath(this.getSavePath())
+ "/" + saveFileName);
if (null != upload) {
String fileresult = FileType(allowType);
System.out.println("上传类型" + fileresult);
if (null != fileresult) {
ActionContext.getContext().put("typerror", "上传的类型不正确!");
return fileresult; // 重新上传
}
copyFile(upload, saveFile);// 上传文件流
return SUCCESS;
}
ActionContext.getContext().put("fileerror", "你还没有选择上传文件!");
return ERROR;
}
// 手动过滤文件类型
public String FileType(String[] types) {
String filetype =this.getExtendsion(uploadFileName);
// 遍历文件类型数组
for (String type : types) {
System.out.println("过滤文件后缀名:"+type+"-filetype="+filetype);
if (type.equals(filetype)) {
return null;
}
}
return INPUT;
}
// 获取上传文件后缀
private String getExtendsion(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos, fileName.length());
}
/**
* 上传文件流处理
*
* @param srcFile
* 源文件
* @param dstFile
* 目标文件
*/
private static void copyFile(File srcFile, File dstFile) {
try {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(srcFile),
BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dstFile),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
while (in.read(buffer) > 0) {
out.write(buffer);
}
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSaveFileName() {
return saveFileName;
}
public void setSaveFileName(String saveFileName) {
this.saveFileName = saveFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
}
2.struts.xml
<?xml version="1.0" encoding="GBK"?>
<!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.i18n.encoding" value="gb2312"></constant>
<!-- 国际化信息配置文件指定 -->
<constant name="struts.custom.i18n.resources" value="message" />
<!-- 系统常量定义,定义上传文件临时存放路径 -->
<constant name="struts.multipart.saveDir" value="D:\"></constant>
<package name ="fileUploadDemo" extends ="struts-default" >
<action name ="fileUpload" class ="com.file.application.action.upload.FileUpload" >
<!-- 配置FileUpload拦截器 -->
<interceptor-ref name ="fileUpload">
<!-- 配置允许上传文件的大小 -->
<param name="maximumSize">20000</param>
</interceptor-ref>
<!-- 配置默认的拦截器 -->
<interceptor-ref name="defaultStack"/>
<param name="savePath">/UploadFiles</param>
<result name ="success">/ShowUpload.jsp </result >
<result name ="input">/FileUpload.jsp</result>
<result name ="error">/error.jsp</result>
</action >
</package >
</struts >
3.web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 定义Struts2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。-->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/fileUpload.action</url-pattern>
</filter-mapping>
</web-app>
这样子就可以完成文件的上传了。。。。。