@PostMapping(value = "/upload")
public String upload(@RequestParam(name = "file") MultipartFile file) throws Exception {
try {
InputStream input = file.getInputStream();
List<Map<String, String>> dataList = ExcelUtil.readExcelForStream(input, 0);
for (Map<String, String> map : dataList) {
String a = map.get("A");
String v = map.get("V");
log.info("A:" + a + " V:" + v);
}
//必须关闭
input.close();
} catch (Exception e) {
log.error(":", e);
throw new RuntimeException(e);
}
return "文件上传成功!";
}
@GetMapping(value = "/download")
public void download(HttpServletResponse response) {
String fileName = UUID.randomUUID() + ".xlsx";
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
try (OutputStream stream = response.getOutputStream();
InputStream inputStream = new FileInputStream("test.xlsx")) {
stream.write(inputStream.readAllBytes());
stream.flush();
} catch (IOException e) {
log.error("文件下载失败:", e);
}
}