This is a simple Jax-rx web service endpoint and client code.
Endpoint:
@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();
}
Client:
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) The Eclipse process does not finish so after some executions a free memory ends. How to stop or disconnect the process?
2) The above code creates a new file in file systems. How to change web-service endpoint to operates with Image object instead of File?