springboot 发送 二进制文件
时间: 2023-10-17 09:17:38 浏览: 84
可以通过使用字节流输出流将二进制文件发送给客户端。具体实现可以参考以下代码:
```
@GetMapping("/download")
public ResponseEntity<Resource> downloadFile() throws IOException {
File file = new File("path/to/binary/file");
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(resource);
}
```
阅读全文