文件下载后台java代码,前后端代码完整版,兼容ie文件下载,且ie下载时页面不会跳转

 前端代码

第一步:在jsp对应的地方加上下面的代码

<iframe style="display: none;" id="download_iframe"></iframe>

 第二步:定义点击事件下载文件

<a id="attachFile" onclick="downLoad('http://downloadFile?fileId=fastuser/M00/00/03/CsL9JFwXPAyATC9OAAkVVGMybN8085.jpg','Hydrangeas.jpg')" href="javascript:void(0)"><i class="icon icon-attach" style="margin-top:4px"></i></a>



function downLoad(url,fileName){
    //对中文文件进行编码,防止ie因为编码问题,而导致的服务没有反应
	var fileName2 = encodeURI(fileName);
    url = url +"&fileName="+fileName2;
	$("#download_iframe").attr("src", url);
} 
    @RequestMapping("/downloadFile")
    public void downloadFile(String fileId,String fileName, HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {      
        byte[] content = null;        
        try {
            content = FDFSUtil.download(fileId);
            response.setContentType("Image/" + fileName); // 设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache"); // 设置响应头信息,告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Content-Disposition", "attachment;filename=" + ExcelUtil.toUtf8String(fileName));
            OutputStream toClient = response.getOutputStream();
            toClient.write(content);
            toClient.flush();
            toClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值