0

I am playing around with Jersey and would like to know how one should implement a "download" feature. For example let's say I have some resources under /files/ that I would like to be "downloaded" via a GET how should I do this? I already know the proper annotations and implementations for GET, PUT, POST, DELETE, but I'm not quite sure how one should treat binary data in this case. Could somebody please point me in the right direction, or show me a simple implementation? I've had a look at the jersey-samples-1.4, but I can't seem to be able to find what I am looking for.

Many thanks!

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • Aren't jersey and restlet different implementations of the REST API? If so, I don't understand why would you want to use both at the same time. Perhaps you meant `REST` and not `Restlet`? – AlikElzin-kilaka Nov 06 '13 at 08:24

1 Answers1

1

You should use @Produces annotation to specify which media type file is (pdf, zip, etc..). Java specification for this annotation can be found here.

Your server should return created file. For example in core java you can do something like this:

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("path")
public StreamingOutput getFile() {
    return new StreamingOutput() {
        public void write(OutputStream out) throws IOException, WebApplicationException {
            try {
                 FileInputStream in = new FileInputStream(my_file);
                 byte[] buffer = new byte[4096];
                 int length;
                 while ((length = in.read(buffer)) > 0){
                    out.write(buffer, 0, length);
                 }
                 in.close();
            } catch (Exception e) {
                throw new WebApplicationException(e);
            }
        }
    };
}
pedjaradenkovic
  • 241
  • 1
  • 9
  • Thanks! How about the case where I don't have the file type in advance, but know it's a binary? I assume the `@Produces` is on the method? (Sorry, would you mind just illustrating it in a method with the proper annotations? I know how to handle the streaming part...) – carlspring Apr 29 '13 at 13:31
  • OK, I edited my post. Now, you have method example. I think that you should use APPLICATION_OCTET_STREAM if your file is binary. In this example I have used post http://stackoverflow.com/a/3503704/987847 – pedjaradenkovic Apr 29 '13 at 14:08
  • I was playing around with the code you provided and I seem to be getting a `returned a response status of 405 Method Not Allowed`. Any ideas...? On the client-side I have `WebResource webResource = client.resource(url); webResource.accept(MediaType.APPLICATION_OCTET_STREAM).get(String.class)` – carlspring May 02 '13 at 15:38
  • 1
    You must specify which HTTP method will implemented service serve. For example I used @GET annotation to specify that for specific "path" and GET HTTP request this method (service) will be executed. – pedjaradenkovic May 07 '13 at 08:47
  • It turned out I had a typo in the @Path and that screwed everything up. Otherwise the code was alright. Thanks anyway! – carlspring May 07 '13 at 09:39