首页 > 编程语言 > 详细

springboot下载文件

时间:2021-05-25 16:59:17      阅读:13      评论:0      收藏:0      [点我收藏+]
1.后端:
public void getExcelTemplate(HttpServletResponse response) throws UnsupportedEncodingException {

   //文件名最好不要用中文,会出现中文乱码
InputStream inputStream = new BufferedInputStream(this.getClass().getResourceAsStream("/template/template.xlsx"));

response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("模板.xlsx", "UTF-8"));

// 循环取出流中的数据
byte[] b = new byte[1024];
int len;
try {
while ((len = inputStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.前端
可以直接使用window.location.href方式打开
downloadTemplate() {
window.location.href = "http://localhost:8080/getTemplate";
}

springboot下载文件

原文:https://www.cnblogs.com/sanshao-ghf/p/14808675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!