服务端是第三方的,我方需要按照接口文档上传文件。
1 、分片上传,实体为 Flux<DataBuffer>
2 、需要携带 header ,媒体类型为 application/json
目前可以确认,服务端是 ok 的,问题出在客户端上传文件的代码。
以下是客户端上传文件的代码:
CompositeByteBuf compositeMetadata = ByteBufAllocator.DEFAULT.compositeBuffer();
// 1. 创建路由元数据
ByteBuf routeMetadata = TaggingMetadataCodec.createTaggingContent(
ByteBufAllocator.DEFAULT, Collections.singletonList(platformConfig.getFileUploadRoute()));
compositeMetadata.addComponent(true, ByteBufAllocator.DEFAULT.buffer().writeBytes(routeMetadata));
// 2. 按文档要求添加请求 header
Map<String, String> uploadFileHeader = new HashMap<>();
uploadFileHeader.put("token", token);
uploadFileHeader.put("fileType", "jpg");
uploadFileHeader.put("fileName", "random-file-name");
ByteBuf customMetadata = ByteBufUtil.writeUtf8(ByteBufAllocator.DEFAULT, JSONUtil.toJsonStr(uploadFileHeader));
CompositeMetadataCodec.encodeAndAddMetadata(compositeMetadata, ByteBufAllocator.DEFAULT,
WellKnownMimeType.APPLICATION_JSON, customMetadata);
// 读取本地文件
Flux<DataBuffer> dataBufferFlux = DataBufferUtils.read(Paths.get(filePath), new DefaultDataBufferFactory(), 1024 * 8);
// 合并 Payloads 将每个 DataBuffer 转换为 Payload ,并附加 metadata
Flux<Payload> requestPayloads = dataBufferFlux.map(dataBuffer -> {
// 将每个 DataBuffer 转换为 Payload ,并附加 metadata
return ByteBufPayload.create(Unpooled.wrappedBuffer(dataBuffer.asByteBuffer()), compositeMetadata);
});
rsocket.requestChannel(requestPayloads)
.doOnNext(payload -> log.error("=====> doOnNext"))
.doOnError(error -> log.error("=====> doOnError", error))
.doOnComplete(() -> log.info("=====> doOnComplete"))
.subscribe();
注:由于对 rsocket 完全不熟,所以以上代码任何地方都有可能是错的。
目前一直报错,缺少请求头:Missing header 'upload-file-header' for method parameter type , 从报错分析,已经请求到接口了,说明路由 metadata 没问题,但是请求头传递不正确。文件数据流是否传递正确还未知。
希望寻求有过 rsocket 相关开发经验的人,帮忙看下代码哪里有问题。
解决后发微信红包 200 元作为报酬。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.