Springboot 下载模板

文件 路径在项目的resources 下面   路径  resources/tempExcel/导入模板.xlsx
后端代码:
/**
* 下载模板
*
* @param response
* @throws IOException
*/
@RepeatSubmit
@PostMapping("/download/template")
public void downloadTemplate(HttpServletResponse response) throws IOException {
Resource resource = new ClassPathResource("tempExcel"+System.getProperty("file.separator")+"导入模板.xlsx");
InputStream fis = resource.getInputStream();
BufferedInputStream bis =new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
//设置响应头信息
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream; charset=UTF-8");
StringBuffer contentDisposition = new StringBuffer("attachment; filename=''");
contentDisposition.append(" ").append("''");
response.setHeader("Content-disposition", contentDisposition.toString());
//边读边写
byte[] buffer = new byte[500];
int i;
while ((i = bis.read(buffer)) != -1) {
os.write(buffer, 0, i);
}
os.flush();

}
 
前端代码

// 导出按钮操作
handleExport() {
let _this = this

let queryParams={}
_this.download('/synchronize/log/download/template', {
...queryParams
}, `导入模板.xlsx`)

}

 
 
posted @ 2024-04-19 14:02  雪儿蛇王  阅读(237)  评论(0编辑  收藏  举报