Java 停止web服务客户端
这是一个简单的Jax-rxweb服务端点和客户端代码 终点:Java 停止web服务客户端,java,web-services,jax-rs,Java,Web Services,Jax Rs,这是一个简单的Jax-rxweb服务端点和客户端代码 终点: @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("
@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服务端点更改为使用图像对象而不是文件进行操作?看看这个问题: 为了在短时间内完成操作,您可能需要超时 这个答案的密码
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
问题从JFrame开始,因此超时不是答案的选项。