Java 使用Jersey 2.x客户端,当服务器仍在写入其输出流时,如何读取输入流?

Java 使用Jersey 2.x客户端,当服务器仍在写入其输出流时,如何读取输入流?,java,jersey-2.0,jersey-client,Java,Jersey 2.0,Jersey Client,我们最近从Jersey 1.x升级到2.x,大部分迁移都进行得很顺利。不过有一个障碍 在1.x中,以下代码允许我们在服务器仍在写入其各自的OutputStream时获取InputStream: final ClientResponse response = webResource .accept(acceptHeader) .get(ClientResponse.class); final InputStream stream = response.getEntity(Input

我们最近从Jersey 1.x升级到2.x,大部分迁移都进行得很顺利。不过有一个障碍

在1.x中,以下代码允许我们在服务器仍在写入其各自的OutputStream时获取InputStream:

final ClientResponse response = webResource
    .accept(acceptHeader)
    .get(ClientResponse.class);
final InputStream stream = response.getEntity(InputStream.class);
/* Process the stream, waiting if necessary */
我们将其用作一种服务器发送事件(在我们发现sse之前),但一个类似且更常见的问题是下载一个大文件。Jersey 2.x代码如下所示:

final Response response = webTarget
    .request()
    .accept(acceptHeader)
    .get();    /* debug shows this call hanging */
final InputStream stream = response.getEntity(InputStream.class);
/* Process the stream, waiting if necessary */

get()方法挂起,因为服务器从未关闭连接。幸运的是,在我们的例子中,服务器只是在等待“事件”发送到客户端,但是如果客户端正在下载一个64 GB的文件…

结果表明问题出在服务器端


在Jersey 1.x中,服务器没有缓冲响应(或者我们已经重写了该行为并忘记了)。解决方案是将属性
jersey.config.contentLength.buffer
设置为
0
。这阻止了服务器缓冲,并且问题中列出的代码未经修改就工作了。

结果表明问题出在服务器端

在Jersey 1.x中,服务器没有缓冲响应(或者我们已经重写了该行为并忘记了)。解决方案是将属性
jersey.config.contentLength.buffer
设置为
0
。这会阻止服务器进行缓冲,并且问题中列出的代码可以正常工作,无需修改