文件准备
//fileCode 传入不同的值判断下载文件
public ResultDTO downloadTemplate(@RequestBody String fileCode) throws Exception {
String fileName = null;
switch (fileCode) {
case "1":
fileName = "ryxxModel.xls";
break;
case "2":
fileName = "GongziMuBan.xls";
default:
}
InputStream inputStream = null;
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
try {
inputStream = this.getClass().getResourceAsStream("/file/" + fileName);
//返回base64给前端
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
Map<String, Object> map = new HashMap<>(8);
map.put("base64", output.toByteArray());
map.put("fileName", fileName);
List returnList = new ArrayList();
returnList.add(map);
return new ResultDTO(0, "下载成功", returnList);
} catch (Exception e) {
e.printStackTrace();
return new ResultDTO(9, "下载失败");
}
}