Spring WebFlux: 如何将文件作为字节数组从数据库返回

huangapple go评论101阅读模式
英文:

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(&quot;/retrieveAttachment/{id}&quot;)
public ResponseEntity&lt;byte[]&gt; downloadFile(@PathVariable String id) throws Exception {
    Attachment attachment = documentManagerService.getAttachment(id);

    return ResponseEntity.ok()
            .contentType(MediaType.parseMediaType(&quot;application/octet-stream&quot;))
            .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;&quot; + attachment.getFileName() + &quot;\&quot;&quot;)
            .body(attachment.getFileAttachment().getData());
}

Now with WebFlux, my documenManagerService returning Mono&lt;Attachment&gt;, this is what I came up with:

public Mono&lt;ServerResponse&gt; getAttachment(ServerRequest request) {
    Mono&lt;byte[]&gt; mono = documentManagerService.getAttachment(request.pathVariable(&quot;id&quot;))
            .map(attachment -&gt; 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&lt;ServerResponse&gt; getAttachment(ServerRequest request) {
    return documentManagerService.getAttachment(request.pathVariable(&quot;id&quot;))
        .flatMap(attachment -&gt; {
            return ServerResponse.ok()
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM.toString())
                .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=\&quot;&quot; + attachment.getFileName() + &quot;\&quot;&quot;)
                .bodyValue(attachment.getFileAttachment().getData())
        });
}

huangapple
  • 本文由 发表于 2020年8月30日 15:24:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63654999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定