public ResponseEntity<FileSystemResource> export(File file) { if (file == null) { return null; } HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attachment;filename=" + file.getName()); return ResponseEntity .ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new FileSystemResource(file)); }
使用方式如下:
@RequestMapping("/downLoadImg")
public ResponseEntity<FileSystemResource> downLoadImg(HttpServletRequest request) {
String imgPath = request.getParameter("img");
//文件路径
String filePath = SDKUtils.getImgPath() + imgPath;
return export(new File(filePath));
}
原文:https://www.cnblogs.com/bdqnquliang/p/11548342.html