import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyFileDownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public static Logger log = Logger.getLogger(MyFileDownloadAction.class);
private String fileName = null;// 显示的文件名
private String path = null;// 要下载文件的路径
public HttpServletRequest request = ServletActionContext.getRequest();
public HttpServletResponse response = ServletActionContext.getResponse();
public String execute() throws Exception {
fileName = request.getParameter("fileName");
path = request.getParameter("path");
InputStream is = null;
try {
is = new FileInputStream(path);
} catch (FileNotFoundException e) {
log.error("文件不存在:" + fileName);
return INPUT;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
OutputStream out = response.getOutputStream();
int b = 0;
while ((b = is.read()) != -1) {
out.write(b);
}
is.close();
out.close();
return NONE;
}
public String getFileName() {
try {
// 解决下载文件中文文件名问题
fileName = new String(fileName.getBytes(), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
}
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class MyFileDownloadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public static Logger log = Logger.getLogger(MyFileDownloadAction.class);
private String fileName = null;// 显示的文件名
private String path = null;// 要下载文件的路径
public HttpServletRequest request = ServletActionContext.getRequest();
public HttpServletResponse response = ServletActionContext.getResponse();
public String execute() throws Exception {
fileName = request.getParameter("fileName");
path = request.getParameter("path");
InputStream is = null;
try {
is = new FileInputStream(path);
} catch (FileNotFoundException e) {
log.error("文件不存在:" + fileName);
return INPUT;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
OutputStream out = response.getOutputStream();
int b = 0;
while ((b = is.read()) != -1) {
out.write(b);
}
is.close();
out.close();
return NONE;
}
public String getFileName() {
try {
// 解决下载文件中文文件名问题
fileName = new String(fileName.getBytes(), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
}
配置文件中
</action>
<action name="myFileDownloadAction" class="包名.MyFileDownloadAction">
</action>