英文:
Spring WebFlux: How to return a file as byte array from DB
问题
刚开始学习Spring Reactive,但我找不到如何做这么简单的事情的示例。在没有WebFlux的情况下,我的控制器看起来像这样:
@GetMapping("/retrieveAttachment/{id}")
public ResponseEntity<byte[]> downloadFile(@PathVariable String id) throws Exception {
Attachment attachment = documentManagerService.getAttachment(id);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileName() + "\"")
.body(attachment.getFileAttachment().getData());
}
现在使用WebFlux,我的documenManagerService
返回Mono<Attachment>
,这是我想出来的:
public Mono<ServerResponse> getAttachment(ServerRequest request) {
Mono<byte[]> mono = documentManagerService.getAttachment(request.pathVariable("id"))
.map(attachment -> attachment.getFileAttachment().getData());
return ServerResponse
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM.toString())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileName() + "\"")
.body(mono, byte[].class);
}
我能够下载文件,但问题是文件没有扩展名,因为我没有设置Content Disposition Header。如何在不进行阻塞调用的情况下设置它?
英文:
Just started leaning Spring Reactive, and I couldn't find any examples on how to do something as simple as this. Without WebFlux, My controller was looking like this:
@GetMapping("/retrieveAttachment/{id}")
public ResponseEntity<byte[]> downloadFile(@PathVariable String id) throws Exception {
Attachment attachment = documentManagerService.getAttachment(id);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileName() + "\"")
.body(attachment.getFileAttachment().getData());
}
Now with WebFlux, my documenManagerService returning Mono<Attachment>
, this is what I came up with:
public Mono<ServerResponse> getAttachment(ServerRequest request) {
Mono<byte[]> mono = documentManagerService.getAttachment(request.pathVariable("id"))
.map(attachment -> attachment.getFileAttachment().getData());
return ServerResponse
.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM.toString())
.body(mono, byte[].class);
}
I'm able to download the file, but the problem is the file is without extension
since I've not set the Content Disposition Header. How do I set it without making a blocking call?
答案1
得分: 3
使用flatMap
可能可以。我正在手机上编写这个,所以还没有尝试过,也没有双重检查任何内容。
public Mono<ServerResponse> getAttachment(ServerRequest request) {
return documentManagerService.getAttachment(request.pathVariable("id"))
.flatMap(attachment -> {
return ServerResponse.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM.toString())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileName() + "\"")
.bodyValue(attachment.getFileAttachment().getData());
});
}
英文:
By using flatMap
maybe. Im writing this on mobile so have not tried it out or double checked anything.
public Mono<ServerResponse> getAttachment(ServerRequest request) {
return documentManagerService.getAttachment(request.pathVariable("id"))
.flatMap(attachment -> {
return ServerResponse.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM.toString())
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileName() + "\"")
.bodyValue(attachment.getFileAttachment().getData())
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论