admin管理员组文章数量:1565829
前端代码
第一步:在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();
}
}
版权声明:本文标题:文件下载后台java代码,前后端代码完整版,兼容ie文件下载,且ie下载时页面不会跳转 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1726427264a1070531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论