gpt4 book ai didi

java - 下载大型结果

转载 作者:行者123 更新时间:2023-12-02 10:47:16 24 4
gpt4 key购买 nike

是否可以使用声明性客户端下载大型结果,例如使用InputStream?我尝试了像

这样的客户端签名
HttpResponse<InputStream> getQueryResult(String jobId, String resultId);

但它尝试下载整个正文,然后导致

io.micronaut.http.client.exceptions.ContentLengthExceededException: The received length exceeds the maximum content length 

提前谢谢您。

最佳答案

这里发生的是,您的客户端请求一个完全接收(聚合)的 HttpResponse,包装一个字节数组,然后将其转换为 InputStream。为了获取不聚合的响应字节,您需要请求一种响应类型,例如 ByteBuffer 的 org.reactivestreams.Publisher (或其合适的子类) >s。然后你需要处理这些。

示例:

Flowable<ByteBuffer<?>> getQueryResult(String jobId, String resultId);

然后,您可以在该 io.reactivex.Flowable 上运行 mapforEachblockingForEach 等- 但请记住释放缓冲区,否则您将生成大量垃圾,并收到令人讨厌的日志消息。示例(Groovy 中):

Flowable<ByteBuffer<?>> responseFlowable = myClient.getQueryResult("job1", "foo")
int sum = 0
responseFlowable.blockingForEach { ByteBuffer byteBuffer ->
sum += byteBuffer.toByteArray().count('!')
((ReferenceCounted)byteBuffer).release() // Let Netty do its thing!
}

(显然,阻塞对于高吞吐量来说是不利的,但这只是一个例子)

我希望这会有所帮助。

关于java - 下载大型结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52457203/

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