spring实现文件下载
时间: 2023-08-31 20:09:57 浏览: 40
好的,关于spring实现文件下载,你可以使用以下代码:
```
@GetMapping("/download")
public ResponseEntity<Resource> downloadFile() {
String fileName = "your_file_name";
Resource resource = new FileSystemResource("/path/to/your/file");
String contentType = null;
try {
contentType = Files.probeContentType(Paths.get("/path/to/your/file"));
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.body(resource);
}
```
其中,`fileName`可以指定下载时的文件名,`/path/to/your/file`指定要下载的文件路径。最终将文件以`Resource`的形式返回给客户端,客户端就可以下载该文件了。
希望以上代码可以帮到您,如果还有什么问题,请继续提问。