朱庇特朱庇特“java阿叔阿叔阿叔阿叔阿叔阿叔阿叔阿叔阿叔阿叔阿叔阿叔""贺盛瑞贺盛瑞,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,阿云,你是说.范思哲是范思哲,范思哲是范思哲!吴亚玲吴亚玲,朱庇特朱庇特朱庇特阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹阿齐兹!
Java(Java)
打包。rainmanqqst。控制器。文件流;
导入com .奥斯特米勒,基地64号:
rg金额。SLF 4j。记录器;
导入rg。SLF 4j。伐木工人工厂;
importrg。springframework。网络。绑定。注释。路径变量:
importrg。springframework。网络。绑定。注释。请求映射:
importrg。springframework。网络。绑定。注释。休息控制器;
importjavax。servlet。servletoutput流:
导入javax。servlet。http。http servletrequest:
导入javax。servlet。http。httpservletresponse:
我会的,我会的。
/**
*@authorRAINMANQQST
* @ 2019-07年日期-151433366052
* @描述
*/
@RestController
@ RequestMapping('/fill ').
publicclassfilecontroller
private statclogger=logloggers。获取记录器(文件控制器。类);
/**
*我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说,我是说.
*文件路径吾曰64号基地哎哎哎哎
*@paramfilePath
*@paramrequest
* @ param响应-设定回应
*/
@ request mapping(value='/resource/{ file path } ')
公共场所屋顶iev resource(@ path变量(' file path ')字符串文件路径,httpservletresponse,httpersvletresponse response responsibility)}
尝试[
字符串路径=base 64。解码(文件路径):
伐木工。信息('资源路径'路径);
文件文件=新文件(文件路径):
伐木工。信息(' filename 3360 '文件。getname());
-r
esponse.setHeader("Content-disposition", "attachment; filename="+file.getName());
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Length",file.length()+"");
InputStream inputStream = new FileInputStream(file);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
while((len=inputStream.read(buffer))>0){
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
inputStream.close();
servletOutputStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
JS下载代码
<html>
<head>
<%--<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">--%>
<script type="text/javascript">
function exportFile() { // name=文件名, blob=文件二进制对象
// 获取时间戳
var timestamp=new Date().getTime();
// 获取XMLHttpRequest
var xmlResquest = new XMLHttpRequest();
// 发起请求
xmlResquest.open("POST", "http://localhost:8086/rainmanqqst/api/system/resource/aHR0cHM6Ly9pbWcxLmRvdWJhbmlvLmNvbS92aWV3L3Bob3RvL2wvcHVibGljL3AxNDIyMDcxNjA4LndlYnA=", true);
// 设置请求头类型
xmlResquest.setRequestHeader("Content-type", "application/json");
// 设置请求token
//xmlResquest.setRequestHeader(
// "Authorization",
// Vue.cookie.get('token')
//);
xmlResquest.responseType = "blob";
// 返回
xmlResquest.onload = function(oEvent) {
var content = xmlResquest.response;
alert(content);
// 组装a标签
var elink = document.createElement("a");
// 设置下载文件名
elink.download = timestamp + ".png";
elink.style.display = "none";
var blob = new Blob([content]);
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
document.body.removeChild(elink);
};
xmlResquest.send();
}
</script>
</head>
<body>
<input onclick="javascript:exportFile()" type="button" value="test" >
</body>
</html>
“java文件流接口和下载的方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/95854.html
