java action下载文件_javaweb-JSP action中附件下载的写法

//TODO 事件委托,下载附件按钮

$(".taskRows").on("click",".downloadAttach",function(){

var attachIndex= $(this).attr("id")//获取当前点击的元素ID

attachIndex = attachIndex.charAt(attachIndex.length-1);

var attach_name= $.trim($("#attach_name"+attachIndex).text());

alert(attach_name);//根据附件名下载附件

if(attach_name!=null &&attach_name!=undefined)

{//$("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?attachName="+attachName);

$("#downloadFrame"+attachIndex).attr("src",baseCtx+"/wpgl/downloadAttachFile.action?Id="+Id+"&attachName="+encodeURI(encodeURI(attachName)));

}

});//action 后台一些主要的包和类

importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.io.UnsupportedEncodingException;importjava.util.ArrayList;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.net.URLDecoder;importjava.net.URLEncoder;/*** 下载文件

*@return*@throwsUnsupportedEncodingException*/@Action(value="downloadAttachFile")publicString downloadAttachFile(){//从前台传过来的附件名字:格式是,“2006#系统测试.docx格式"

String downloadName =wpyId+"#"+attachName;//传入的数据

System.out.println(downloadName);try{

OutputStream os= this.response.getOutputStream();//OutputStream os = this.getResponse().getOutputStream();

if (Util.isIE(this.request)) //判断客户端是否为IE

{//编码

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

}else{

downloadName= new String(downloadName.getBytes("UTF-8"),"iso-8859-1");

}//设置让浏览器弹出下载对话框的Header

this.response.setContentType("application/x-download");this.response.addHeader("Content-Disposition","attachment;filename=\"" + downloadName + "\"");this.response.flushBuffer();//将前台的数据经过包装后(加了头数据,并经过编码)需要解码

String downloadName1 =URLDecoder.decode(downloadName, "UTF-8");

System.out.println(downloadName1);//解码的结果输出

String bgFile=realPath + File.separator +downloadName1;

FileInputStream fis= newFileInputStream(bgFile);

Util.copyStrem(fis, os);

fis.close();

os.close();

System.out.println("==========成功了!!===========");

}catch(IOException e) {

e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因

System.out.println("==========出错了!!===========");

}returnNONE;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值