0

I have REST Webservice which give me the file from the system:

@Stateless
@Path("/print")
public class PictureWebservice {

    @GET
    @Path("/startPrint")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getFile() {

        String path = "/mypath.JPG";
      File file = new File(path);
      return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
          .header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional
          .build();
    }
}

If I open my browser and type in: http://192.168.2.11:8080/rest/print/startPrint

=> Everything works fine and I got the image.

But now I would like to have my file on the other PC: File file = new File("http://192.168.2.11:8080/rest/print/startPrint")

But than I got an error "FileNotFoundException". What is wrong? I guess the path is not valid?

internet
  • 385
  • 1
  • 8
  • 27

2 Answers2

0

You can use a Restful Client like Resteasy.

    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target("http://localhost:8080/rest/status");
    Response response = target.request().get();
    String value = response.readEntity(String.class);
    System.out.println(value);
    response.close();
ceeleP
  • 21
  • 1
0

File is not suitable for this situation.

To download the image, you have at least two approaches:

Using URL

URL url = new URL("http://192.168.2.11:8080/rest/print/startPrint");
InputStream is = new BufferedInputStream(url.openStream();

Using JAX-RS Client API

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://192.168.2.11:8080/rest")
                         .path("print")
                         .path("startPrint");
Response response = target.request().get();
InputStream is = response.readEntity(InputStream.class);

Since you have an InputStream, just write it to a file:

Path path = Paths.get("C:/temp", "download.png");
Files.copy(is, path);
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • Thank you - but I got a compile error here: InputStream is = response.readEntity(InputStream.class); This is the error message: The method readEntity(Class) is undefined for the type Response – internet Nov 22 '15 at 15:40
  • @internet Ensure you've imported the [`javax.ws.rs.core.Response`](http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.html) class. – cassiomolin Nov 22 '15 at 15:44
  • yes this here: javax.ws.rs javax.ws.rs-api 2.0.1 – internet Nov 22 '15 at 15:45
  • @internet I'm sure [this method exists](http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.html#readEntity-java.lang.Class-java.lang.annotation.Annotation:A-). – cassiomolin Nov 22 '15 at 15:48
  • @internet Please give me more details about your environment. What's your JAX-RS implementation? Jersey? RESTEasy? What's your webserver? – cassiomolin Nov 22 '15 at 16:02
  • @internet Have a look [here](https://www.eclipse.org/forums/index.php/t/641518/). Basically, the method `Response.readEntity()` was added in JAX-RS 2.0 (JEE 7) but was absent in earlier versions (for example, JEE 6). If you are using Jersey as JAX-RS implementation, ensure you don't have Jersey 1.x dependencies in your `pom.xml`. – cassiomolin Nov 22 '15 at 16:09
  • there are only 1.x dependencies? http://mvnrepository.com/artifact/com.sun.jersey/jersey-client – internet Nov 22 '15 at 16:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95855/discussion-between-internet-and-cassio-mazzochi-molin). – internet Nov 22 '15 at 16:56
  • @internet That's the problem. You are using the incorrect dependency for the client API. Replace [this](http://mvnrepository.com/artifact/com.sun.jersey/jersey-client) dependency with [this](http://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client). – cassiomolin Nov 22 '15 at 16:57
  • @internet Please be clear. You have the code in front of you. I don't. – cassiomolin Nov 22 '15 at 17:06
  • Just another question: if I use by URL (your first idea). After the printing I should delete the file? correct? – internet Nov 22 '15 at 20:53
  • @internet Independent the approach you use, you can delete the file if you need: `Files.delete(path);`. – cassiomolin Nov 22 '15 at 21:04
  • @internet Just came to my mind you can create a temporary file: `Path path = Files.createTempFile("temp-", ".tmp"); path.toFile().deleteOnExit();` – cassiomolin Nov 22 '15 at 22:15
  • When should I execute deleteOnExit()? Get File() -> print() -> delete()? But what is if the print is not finish and then the delete() will be execute? – internet Nov 22 '15 at 22:20
  • @internet You should spend some time *reading the documentation*. According to the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/io/File.html#deleteOnExit--), when your create a temporary file, you should invoke [`File.deleteOnExit()`](http://docs.oracle.com/javase/8/docs/api/java/io/File.html#deleteOnExit--), which will requests the file to be deleted when the virtual machine terminates. You can invoke it just after creating the file. – cassiomolin Nov 22 '15 at 22:38
  • @internet If my answer is useful and answers your original question, please consider upvoting and accepting it. – cassiomolin Nov 24 '15 at 16:49