文件地址放在配置文件里
获取文件地址
@Value("${一级路径.templeteFile}")
private String filePath;
后端接口编写【目前没有遇见中文压缩包乱码问题】
@PostMapping("/download")
private void fileDown(HttpServletResponse response) throws FileNotFoundException {
File file = new File(filePath);
if(!file.exists()){
// 文件不存在异常
throw new FileNotFoundException("文件不存在: " + filePath);
}
String fileName="import-Template";
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
try {
InputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
// 创建输出流,将文件内容写入响应输出流中
OutputStream outputStream = response.getOutputStream();
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
}
// 关闭输入流和输出流
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}