@GetMapping("/download")
public void InfoDownload(HttpServletResponse response) throws IOException {
ClassPathResource resource = new ClassPathResource("xxxx/导入模板.xlsx");
//设置下载后的文件名 这里设置不会出现乱码
String fileName = "元素导入模板.xlsx";
fileName = URLEncoder.encode(fileName, "UTF-8");
// 配置文件下载 上面有后缀这里又加了一次后缀 这样下载才会带后缀名
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName+ ".xlsx;filename*=utf-8''"+fileName+".xlsx");
//设置下载文件格式 是什么格式就去百度找相对应的文件格式
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("UTF-8");
byte[] buffer = new byte[1024];
//文件输入流
InputStream fis = null;
BufferedInputStream bis = null;
//输出流
OutputStream os = null;
try {
os = response.getOutputStream();
fis = resource.getInputStream();
bis = new BufferedInputStream(fis);
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer);
i = bis.read(buffer);
}
} catch (Exception e) {
log.error("输出流失败", e);
}
try {
bis.close();
fis.close();
} catch (IOException e) {
log.error("流关闭失败", e);
}
}