java struts2下载文件_java struts2 的 文件下载

jsp:

pageEncoding="UTF-8"%>

Insert title here

点击下载

struts.xml

text/plain

inputStream

attachment;filename="${downloadFileName}"

4096

com.hdsx.gzgispf.controller.FileDownloadAction  :

/**

* 处理带中文的文件名(可设置下载文件名)

* @return

*/

public String getDownloadFileName() {

fileName="struts2的下载文件.doc";

String downloadFileName = null;

try {

downloadFileName = fileName;

downloadFileName = URLEncoder.encode(downloadFileName,"UTF-8");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return downloadFileName;

}

/**

* 获取下载流

* @return

*/

public InputStream getInputStream () {

String context="";

String str="";

InputStream in=null;

//获取当前项目的路径    被下载的文件名为:111.doc

String path = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/")+"111.doc";

try {

File someFile = new File(path );

FileInputStream fis = new FileInputStream(someFile);

InputStreamReader isr = new InputStreamReader(fis,"UTF-8");

BufferedReader br = new BufferedReader(isr);

while((str=br.readLine())!=null){

context+=str+"\n";

}

in=new ByteArrayInputStream(context.getBytes());

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return in ;

}

public String execute(){

return "success";

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值