struts2响应文件 struts2下载txt

Posted 鲲鹏展翅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2响应文件 struts2下载txt相关的知识,希望对你有一定的参考价值。

public class txtAction extends ActionSupport {

private String type;

@Action(value="getTxt")
public String getTxt() {
if("txt".equals(type)){
String restxt="鲲鹏展翅";

ServletActionContext.getResponse().setContentType("text/plain");

ServletActionContext.getResponse().setCharacterEncoding("gbk");
ServletActionContext.getResponse().setHeader("connection", "close");
ServletActionContext.getResponse().setHeader("Content-Disposition","attachement;filename="+System.currentTimeMillis()+".txt");

try {
InputStream is = new ByteArrayInputStream(restxt.getBytes("UTF-8"));

//通过response获得输出流
OutputStream os=ServletActionContext.getResponse().getOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=is.read(b))!=-1){
os.write(b, 0, len);
}
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}


public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

}





























以上是关于struts2响应文件 struts2下载txt的主要内容,如果未能解决你的问题,请参考以下文章

struts2文件上传和下载

使用struts2和poi导出excel文档

深入分析JavaWeb Item47 -- Struts2拦截器与文件上传下载

struts2学习(14)struts2文件上传和下载多个文件上传和下载

struts2学习(13)struts2文件上传和下载

基于Struts2框架的文件下载 --- Struts2