java - WebFlux 如何以及在何处保存文件?
问题描述
从前端我得到一张我保存到资源目录的图片。但是我只能在重新启动服务器后才能恢复。因此,存在一个关于将文件保存在哪里以便您可以通过前端链接立即接收它们的问题。
在控制器中保存文件: ....
String uploadDir = "./src/main/resources/static/img/";
file.transferTo(new File(uploadDir )).subscribe();
如果我需要将图像保存到磁盘上的单独文件夹中,请执行以下操作?:
在控制器中保存文件:
String uploadDir = "C:/temp/";
file.transferTo(new File(uploadDir )).subscribe();
并进入前端 <img src="img/temp.png">
:
@GetMapping("/img/{id}")
public Mono<Void> downloadByWriteWith(@PathVariable String id, ServerHttpResponse response) throws IOException {
ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
// response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=parallel.png");
response.getHeaders().setContentType(MediaType.IMAGE_PNG);
File file = new File("C:/temp/" + id);
return zeroCopyResponse.writeWith(file, 0, file.length());
}