excel文件放在Resource下面
@PostMapping("/downloadAddCardFtl")
@OperateLog(module = Module.CARD, menu = "移动卡列表", operate = "下载导卡OR分配卡模板")
@ApiOperation("下载导卡OR分配卡模板")
@PreAuthorize("authenticated and hasPermission('or','card_add,card_assign')")
public void downloadFtl(HttpServletResponse response, DownLoadCardFtlType type) {
try {
if (null == type) {
log.error("下载导卡模板失败,type:【{}】", type);
return;
}
//获取要下载的模板名称
String fileName = null;
switch (type) {
case ADD_CARD:
fileName = "addCard.xlsx";
break;
case ASSIGN_CARD:
fileName = "assignCard.xlsx";
break;
default:
break;
}
// 设置要下载的文件的名称
response.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
// 通知客服文件的MIME类型
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
// 获取文件的路径
String path = CardController.class.getClassLoader().getResource(fileName).getPath();
System.out.println(path);
FileInputStream input = new FileInputStream(path);
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = input.read(b)) != -1) {
out.write(b, 0, len);
}
// 修正 Excel在“xxx.xlsx”中发现不可读取的.内容。是否恢复此工作薄的内容?如果信任此工作簿的来源,请点击"是"
response.setHeader("Content-Length", String.valueOf(input.getChannel().size()));
input.close();
} catch (Exception e) {
log.error("下载导卡模板失败", e);
}
}