首页 > 解决方案 > Webflux Webclient 未设置 Content-Range 标头

问题描述

使用flux Webclient,我正在尝试org.springframework.core.io.buffer.DataBuffer将从另一个Webclient 下载的文件流式传输到新端点。

此端点(我无法控制)需要在上传时设置 2 个标头:Content-LengthContent-Range.

如果我按照下面的硬编码测试手动设置它们,一切顺利。随着 Flux 数据缓冲区的上传,我不确定从哪里开始动态设置这些设置。

    public Mono<ResourceResponse>uploadFile(URI destination, Flux<DataBuffer> inputFile){
        WebClient webClient = WebClient.create();
        return webClient.put()
                .uri(destination)
//                .header(HttpHeaders.CONTENT_LENGTH, "12")
//                .header(HttpHeaders.CONTENT_RANGE, "bytes 0-11/12")
                .body(BodyInserters.fromDataBuffers(inputFile))
            .retrieve()
            .bodyToMono(ResourceResponse.class);
    }

我应该在客户端上做更多的事情来提取这些标头吗?

    public Flux<DataBuffer> downloadFile(URI uri) {
        return botClient.get().uri(uri)
                .accept(MediaType.APPLICATION_OCTET_STREAM)
                .retrieve().bodyToFlux(DataBuffer.class);
    }

标签: spring-bootspring-webflux

解决方案


推荐阅读