On Tue, Sep 7, 2010 at 2:54 PM, Kevin Duffey <andjarnic_at_yahoo.com> wrote:
> Hi all,
>
> I went to the site today to try to find the addons to jersey.. want to look
> at working with sending binary data like files/images via a REST call. The
> downloads links all seem to go to Chapter 7 Dependencies now.
>
> Anyway, despite that being broken, has anyone worked with sending files,
> images, large binary data, up via REST? I know Craig wrote an addon a while
> ago, just curious with all the changes lately if anyone has an elegant
> solution to sending files to REST and vice versa, getting files back from
> REST.
>
> Thanks.
>
>
For upload and download of binary data by itself, the simplest thing to do
is accept an InputStream argument (for upload), or produce an entity that is
an InputStream (for download):
@Path("/foo")
public class MyResourceClass {
@GET
@Produces("image/png")
public Response download(...) {
InputStream stream = ... InputStream pointing at my image data
...
return Response.ok().entity(stream).build();
}
@POST
@Consumes("image/png")
public void upload(InputStream stream) {
... read from stream and store the image somewhere ...
}
This works because JAX-RS includes MessageBodyReader and MessageBodyWriter
implementations that know what to do with an InputStream.
If you want to send metadata about the image along with the image itself,
you can use the multipart extension stuff, but the same principle applies
... you'll use an InputStream to deal with the multipart body part that
contains the binary data.
Craig