1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
@RequestMapping(value = "/io", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }) public ResponseEntity<InputStreamResource> download(@RequestParam(value = "id") Integer id) throws Exception {
val filePath = "D:/file/haha1534299140658.csv";
val file = new FileSystemResource(filePath); HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename())); headers.add("Pragma", "no-cache"); headers.add("Expires", "0");
try { return ResponseEntity .ok() .headers(headers) .contentLength(file.contentLength()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(file.getInputStream())); } catch (IOException e) { e.printStackTrace(); log.info("==== 文件下载出现异常:IOException::位置:HFileController.download/GET ==="); } return null; }
|