文件上传:
@PostMapping(value = "/upload")
public Mono<String> requestBodyFlux(@RequestPart("filePart") FilePart filePart) throws IOException {
System.out.println(filePart.filename());
Path tempFile = Files.createTempFile("tmp", filePart.filename());
AsynchronousFileChannel channel =
AsynchronousFileChannel.open(tempFile, StandardOpenOption.WRITE);
DataBufferUtils.write(filePart.content(), channel, 0)
.subscribe();
System.out.println(tempFile.toString());
return Mono.just(filePart.filename());
}
文件下载:
@GetMapping("/download")
public Mono<Void> downloadByWriteWith(ServerHttpResponse response) throws IOException {
ZeroCopyHttpOutputMessage zeroCopyResponse = (ZeroCopyHttpOutputMessage) response;
response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=cert.crt");
response.getHeaders().setContentType(MediaType.APPLICATION_OCTET_STREAM);
Resource resource = new ClassPathResource("cert.crt");
File file = resource.getFile();
return zeroCopyResponse.writeWith(file, 0, file.length());
}