@RestController@RequestMapping("file")@Api(tags ="下载文件")publicclassdownloadFile{@GetMapping("/downloadFile")public ResponseEntity<FileSystemResource>downloadFile(String path){returnexport(newFile(path));}public ResponseEntity<FileSystemResource>export(File file){if(file == null){return null;}
HttpHeaders headers =newHttpHeaders();
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",newDate().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));return ResponseEntity
.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(newFileSystemResource(file));}}前端传入路径,就可以直接下载到浏览器了。


发布评论