本文介绍了使用JAX-RS通过HTTP PUT流传输字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作流程,涉及从Java客户端到Web服务器的HTTP POST.帖子的主体具有规范对象.然后,我将其从我的Web服务器传递到运行大量计算的Apache ZooKeeper(后者在服务器上以其自己的进程运行).我正在努力弄清楚如何以流方式将字节发送回我的Web服务器.我需要它流回,因为我的Java客户端在Web服务器上有一个HTTP GET请求,正在等待流回字节.我不能等待整个计算完成,我希望尽快将字节发送回客户端.

I have a workflow that involves doing a HTTP POST from my Java client to my web server. The body of the post has a specification object. I then pass that on from my webserver to Apache ZooKeeper (which runs in its own process on the server) that runs a big hairy calculation. I am struggling with figuring out how to send back the bytes to my webserver in streaming fashion. I need it to stream back because I have a HTTP GET request on my webserver from my Java client that is waiting to stream back the bytes. I cannot wait for the whole calculation to finish, I want bytes sent as soon as possible back to the client.

JAX-RS的大多数在线示例都是从客户端和Web服务器端进行HTTP PUT的,而没有流代码示例.我将发布到目前为止的内容,但不起作用.

Most the examples online for JAX-RS that do a HTTP PUT from the client side and on the webserver side don't have examples for streaming code. I'll post what I have so far, but it doesn't work.

这是我的ZooKeeper Java代码,它调用JAX-RS客户端PUT.我真的不确定该怎么做,我从未尝试过使用JAX-RS传输数据.

Here is my ZooKeeper Java code, which calls a JAX-RS client-side PUT. I am really unsure of how to do this, I have never tried streaming data with JAX-RS.

final Client client = ClientBuilder.newClient();
final WebTarget createImageTarget = client.target("groups/{imageGroupUuid:" + Regex.UUID + "}");
StreamingOutput imageResponse = createImageTarget.request(MediaType.APPLICATION_OCTET_STREAM).put(Entity.entity(createRandomImageDataBytes(imageConfigurationObject), MediaType.APPLICATION_OCTET_STREAM), StreamingOutput.class);

这是我的Web服务器代码,用于处理HTTP PUT.这只是一个存根,因为我对客户端HTTP PUT没有信心.

Here is my webserver code which handles the HTTP PUT. It is just a stub because I have no confidence in my client side HTTP PUT.

@PUT
@PATH("groups/{uuid:" + Regex.UUID + "}")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public void updateData(StreamingOutput streamingOutput)
{

}

推荐答案

尝试如下操作:

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/{arg}")
public Response get(@PathParam("arg") {

    //get your data based on "arg"

    StreamingOutput stream = new StreamingOutput() {
        @Override
        public void write(OutputStream os) throws IOException, WebApplicationException {
            Writer writer = new BufferedWriter(new OutputStreamWriter(os));

            for (org.neo4j.graphdb.Path path : paths) {
                writer.write(path.toString() + "\n");
            }
            writer.flush();
        }
    };

    return Response.ok(stream).build();
}

@PUT
@Consumes("application/octet-stream")
public Response putFile(@Context HttpServletRequest request,
                     @PathParam("fileId") long fileId,
                     InputStream fileInputStream) throws Throwable {
    // Do something with the fileInputStream
    // etc
}

这篇关于使用JAX-RS通过HTTP PUT流传输字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:58