停止Web服务客户端

问题描述 投票:0回答:1

这是一个简单的Jax-rs Web服务端点和客户端代码。

终点:

@GET
@Path("/image")
@Produces("image/jpg")
public Response getCustomerDataFile() {
    String path = "c:\\image.jpg";
    File file = new File(path);
    ResponseBuilder rb = Response.ok((Object) file);
    rb.header("Content-Disposition", "attachment; filename=imageServise.jpg");
    return rb.build();
}

客户:

try {
        Client client = Client.create();
        WebResource resource = client.resource("http://webservice.com/rest/download");
        ClientResponse response = resource.accept("image/png").get(ClientResponse.class);

        if (response.getStatus() == 200) {

            System.out.println("ok");
            File file = response.getEntity(File.class); //save temp file in file system
            System.out.println(file); //C:\DOCUME~1\j3bfd\LOCALS~1\Temp\rep2011533673549634609tmp

            Image img = ImageIO.read(file);
            JFrame frame = new JFrame();
            frame.setSize(300, 300);
            JLabel label = new JLabel(new ImageIcon(img));
            frame.add(label);
            frame.setVisible(true);

        } else
            System.out.println("Something went wrong");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

1)Eclipse进程没有完成,因此在一些执行后,可用内存结束。如何停止或断开进程?

2)上面的代码在文件系统中创建一个新文件。如何更改Web服务端点以使用Image对象而不是File操作?

java web-services jax-rs
1个回答
0
投票

看看这个问题:

Setting request timeout for JAX-RS 2.0 Client API

也许超时是您在短时间内完成操作所需的。

该答案的代码

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
© www.soinside.com 2019 - 2024. All rights reserved.