/**
* 下载航线信息导入模板
*/
@GetMapping(value = "/downloadTemplateNew")
@ApiOperation(value = "下载航线信息导入新版模板")
public void downloadStationInfoTemplateNew(HttpServletRequest request, HttpServletResponse response){
try {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String fileName = "新版航线模板.xlsx";
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(fileName, "UTF-8"));
// 这种方法读取文件不行
// ClassPathResource classPathResource = new ClassPathResource("template/" + fileName);
InputStream inputStream = RouteInfoController.class.getClassLoader().getResourceAsStream("template/" + fileName);
// // 可以一开始就看到文件大小
//
// InputStream inputStream = classPathResource.getInputStream();
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[1024 * 1024 * 10];
int length = -1;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
文件名乱码,项目打包上传到服务器下载文件损坏
于 2022-01-13 11:32:46 首次发布