0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
andy007
  • 907
  • 1
  • 15
  • 41

1 Answers1

0

Take a look at this question:

Setting request timeout for JAX-RS 2.0 Client API

Maybe a timeout is what you need in order to finish the operation after a short while.

Code from that answer

ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);
Community
  • 1
  • 1
Francisco Hernandez
  • 2,378
  • 14
  • 18