download.html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>DOWNLOAD FILES PAGES</title>
</head>
<body>
<form method="post" action="/download/test-download!doDownload.shtml" theme="simple"">
<input type="text" />
<input type="submit" value="DOWNLOAD" />
</form>
<br/>
<form method="post" action="/download/test-download!doDownloadX.shtml" theme="simple"">
<input type="text" />
<input type="submit" value="DOWNLOAD" />
</form>
</body>
</html>
java code TestDownloadAction.java
package com.test.view.download;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import com.opensymphony.xwork2.ActionSupport;
@Results( {
@Result(name = "download", type = "stream", params = { "contentType", "text/plain", "contentDisposition", "attachment;filename=\"${fileName}\"", "bufferSize", "2048" })
})
public class TestDownloadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 8195276150101182336L;
private String fileName;
private String data;
private InputStream inputStream;
private OutputStream outputStream;
private static String srcData = "0序号,1乘客姓名,2乘客类型,3证件号,4证件类别\n\r0,测试乙,ADT,420104198710011611,NI\n\r1,测试甲,ADT,420104198710011611,NI";
/** doXxxxx()********************************************************************** */
public String doDownload() {
this.setFileName ("downloadfile.csv");
this.setData(srcData);
return "download";
}
public void doDownloadX(){
try {
this.setFileName ("downloadfile.csv");
this.setData(srcData);
//确保IE识别本次为下载文件
ServletActionContext.getResponse().setHeader("Content-Transfer-Encoding","binary");
ServletActionContext.getResponse().setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
ServletActionContext.getResponse().setContentType("text/plain;charset=iso8859-1");//UTF-8");
ServletActionContext.getResponse().addHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("gb2312"),"iso8859-1")); //确保IE识别本次为下载文件
ServletActionContext.getResponse().setCharacterEncoding("gb2312");//("utf8");
ServletActionContext.getResponse().getWriter().print(data);
} catch (IOException e) {
e.printStackTrace();
}
return ;
}
/** **getters and setters************************************************* */
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public InputStream getInputStream() {
//return inputStream;
//InputStream in=ServletActionContext.getServletContext().getResourceAsStream(realPath);
//System.out.println(in);
InputStream stream = new ByteArrayInputStream(data.getBytes());
return stream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public void setOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
}
项目中用来测试文本下载,贴出求拍砖