停止Web服务客户端

时间:2015-11-24 20:37:10

标签: java web-services jax-rs

这是一个简单的Jax-rx 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?

1 个答案:

答案 0 :(得分: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);