users@jersey.java.net

Re: [Jersey] Working with binary files (images/files, etc) via REST

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Tue, 14 Sep 2010 17:59:33 -0700

On Sep 14, 2010, at 5:45 PM, Charles Overbeck wrote:

> I'm just implementing the uploading myself for the first time. In
> addition to the actual bytes, the only metadata I need is the name
> being given to those bytes, e.g., foo.jpg, goo.doc, etc. I'm
> thinking that instead of having to use the multipart extension, I
> could just pass the name in the HTTP header, something like Content-
> Disposition: filename=foo.jpg.
>
> Is there any reason this is a bad idea/design? I haven't tried it
> yet, but it seems like it should work.
>

In principle i don't think there is anything wrong with it, although i
do not know how common it is applied as an HTTP request header.

Note that the Content-Disposition header should have a disposition
type then a set of parameters:

   http://en.wikipedia.org/wiki/MIME#Content-Disposition

so reusing the type of "attachment" seems reasonable for file upload.


> Something like:
> @Context
> protected HttpHeaders httpHeaders;
> ..
>
> @POST
> @Consumes("application/octet-stream")
> public void upload(InputStream stream) {
> String name =
> getFileNameFromHeader(httpHeaders.getRequestHeader("Content-
> Disposition"));
> // Read from stream and save bytes with name in database.
> }
>

Try the following instead:

   @POST
   @Consumes("application/octet-stream")
   public void upload(InputStream stream, @HeaderParam("Content-
Disposition ") ContentDisposition cd) {
      String name = cd.getFileName();
      // Read from stream and save bytes with name in database.
   }

Paul.