springboot+vue导出本地可执行文件
1、前端页面增加下载链接
<a href="http://localhost:80/system/download" download="xxx.exe">下载地址</a>
2、后端读取文件下载
//下载文件
@GetMapping("/system/download")
public void download(HttpServletResponse response) {
String filepath ="E:/file";
File file = new File(filepath + "/xxx.exe");
byte[] buffer = new byte[1024];
BufferedInputStream bis = null;
OutputStream os = null;
try {
//文件是否存在
if (file.exists()) {
//设置响应
response.setContentType("application/octet-stream;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=xxx.exe");//写入文件具体名称
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
while (bis.read(buffer) != -1) {
os.write(buffer);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、将下载路径配置放入无需验证列表,放入SecurityConfig.java中,否则报错401
.antMatchers("/system/download").permitAll()
浙公网安备 33010602011771号