OutputStream os = null;
InputStream in = null;
String fileName = "score_template.xlsx";
//获取当前项目resources下templates目录下的指定名称文件
ClassPathResource resource = new ClassPathResource("\\templates\\" + fileName);
try {
response.setContentType("application/vnd.ms-excel");
//做中文乱码浏览器兼容处理
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > -1) {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));
} else {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName,"utf-8"));
}
in = resource.getInputStream();
os = response.getOutputStream();
//复制
IOUtils.copy(in, response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
System.out.println("下载文件失败," + e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
}
}
经验系列之打jar包之后下载项目目录下的指定文件
于 2022-01-21 17:18:19 首次发布