@ApiOperation(value = "文件导出")
@GetMapping(path = "/api/download")
public ResponseEntity<Resource> download(@RequestParam("文件ID") String id) {
try {
String directory = System.getProperty("user.home") + "/app/download";
File file = new File(directory, id + ".xlsx");
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
Path path = Paths.get(file.getAbsolutePath());
Resource resource = new ByteArrayResource(Files.readAllBytes(path));
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.body(resource);
} catch (Exception e) {
log.error(e.getMessage(), e);
return ResponseEntity
.status(401)
.build();
}
}