只需新建一个类继承抽象类HttpDownloadAction ,实现抽象方法getRootDirectory();同时将filename传进来即可。
HttpDownloadAction :
package com.---.downloadManager;
import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.webwork.interceptor.ServletResponseAware;
import com.opensymphony.xwork.ActionSupport;
import com.---.DownloadConfig;
public abstract class HttpDownloadAction extends ActionSupport
implements ServletResponseAware, ServletRequestAware {
public abstract String getRootDirectory();
private static final long serialVersionUID = 1L;
private HttpServletResponse response;
private HttpServletRequest request;
private String prefix;
private DownloadConfig downloadConfig;
private String filename;
private String message;
private String fileDir;
public String execute() throws Exception {
fileDir = this.getRootDirectory();
if (filename == null || filename.equals("")) {
message = "文件下载路径为空!";
return SUCCESS;
}
filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");
String fileDownLoadPath = getFileDownloadPath() ;
File file = new File(fileDownLoadPath);
if (!file.exists()) {
message = "文件" + filename + "不存在!";
return SUCCESS;
}
FileInputStream fis = null;
ServletOutputStream os = null;
try {
prefix = filename.substring(filename.lastIndexOf(".")+1).toLowerCase();
Map<String, String> str = downloadConfig.getResponseContentType();
response.setContentType(downloadConfig.getResponseContentType().get(prefix));
String userAgent = request.getHeader("User-Agent");
if(userAgent.indexOf("MSIE") != -1)
filename = "filename=" + java.net.URLEncoder.encode(filename,"UTF-8");
else
filename = "filename*=utf8''" + java.net.URLEncoder.encode(filename,"UTF-8");
response.setHeader("content-Disposition", "attachment;" + filename);
os = response.getOutputStream();
fis = new FileInputStream(fileDownLoadPath);
byte[] buff = new byte[1024];
int n = 0;
while ((n = fis.read(buff)) != -1) {
os.write(buff, 0, n);
}
return NONE;
} catch (Exception e) {
System.out.println(e+"...");
message = "文件" + filename + "不存在!";
} finally {
if(fis != null)
fis.close();
if(os != null)
os.flush();
}
return SUCCESS;
}
private String getFileDownloadPath() {
if(fileDir == null || fileDir.equals("")){
return filename;
}else{
return fileDir + "\\" + filename;
}
}
public void setServletResponse(HttpServletResponse reponse) {
response = reponse;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public DownloadConfig getDownloadConfig() {
return downloadConfig;
}
public void setDownloadConfig(DownloadConfig downloadConfig) {
this.downloadConfig = downloadConfig;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
DownloadConfig:
package com.---.config;
import java.util.Map;
public class DownloadConfig {
private Map<String,String> responseContentType;
public Map<String, String> getResponseContentType() {
return responseContentType;
}
public void setResponseContentType(Map<String, String> responseContentType) {
this.responseContentType = responseContentType;
}
}
downloadConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http//www.springframework.org/dtd/spring-beans.dtd"> <beans default-autowire="byName"> <bean id="downloadConfig" class="com.viathink.lims.center.config.DownloadConfig" singleton="true"> <!-- 下载文件时不同文件类型对应的response contentType配置 --> <property name="responseContentType"> <map> <entry key="doc"><value>application/msword</value></entry> <entry key="xls"><value>application/vnd.ms-excel</value></entry> <entry key="txt"><value>text/plain</value></entry> </map> </property> </bean> </beans>