gpt4 book ai didi

java - Play 应用程序 |如何将 FilePart 传递给其他请求

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:26 25 4
gpt4 key购买 nike

在java play应用程序中,我公开了文件的Put请求,并且我想在wsClient的另一个请求中使用此FilePart:

public CompletionStage<String> upload() {
Http.RequestBody body = request().body();
Http.MultipartFormData formData = body.asMultipartFormData();
Http.MultipartFormData.FilePart<File> file = formData.getFile("file");

return wsClient.url("fake")
.setContentType("multipart/form-data")
.post(Source.single(b))
.thenApplyAsync(wsResponse -> {
return wsResponse.getBody();
})
.exceptionally(throwable -> throwable.getMessage());
}

我得到了响应:“java.lang.UnsupportedOperationException:不支持的部件类”

感谢您的帮助

最佳答案

此帖子端点需要以下类型的正文:

Source<? super Http.MultipartFormData.Part<Source<ByteString, ?>>, ?>

可以通过以下方式从表单部件列表构建:

List<Http.MultipartFormData.Part> partList = new ArrayList<>();
Source<ByteString, ?> file = FileIO.fromFile(filePart.getFile());
Http.MultipartFormData.FilePart<Source<ByteString, ?>> filePart = new Http.MultipartFormData.FilePart<>(file.getKey(), file.getFilename(), filePart.getContentType(), file);
partList.add(filePart);

然后,您可以使用以下命令将其作为帖子正文提供:

.post(Source.from(Collections.unmodifiableList(partList)))

关于java - Play 应用程序 |如何将 FilePart 传递给其他请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com