springboot 实现文件下载功能
文件存在在data目录下
@GetMapping(value = "/file/download") public ResponseEntity<FileSystemResource> getFile(@RequestParam String fileName) throws FileNotFoundException { String path = System.getProperty("user.dir")+ "/data/"; File file = new File(path + fileName); if (file.exists()) { return export(file); } System.out.println(file); return null; } public ResponseEntity<FileSystemResource> export(File file) { if (file == null) { return null; } HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Content-Disposition", "attachment; filename=" + file.getName()); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); headers.add("Last-Modified", new Date().toString()); headers.add("ETag", String.valueOf(System.currentTimeMillis())); return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file)); }