users@jersey.java.net

Re: [Jersey] File upload with Jersey

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 24 Feb 2009 16:17:44 +0100

On Feb 24, 2009, at 2:53 PM, Robert Naczinski wrote:

> Hi,
>
> thank you for your answer. I'm using Jakarta HttpClient.
>
> It must also be possible to use HTML Form or Jersey Client API.
>

OK so the client is sending multipart/form-data.

You can do the following:

   @Consumes("multipart/form-data")
   @POST
   public void post(@FormParam("file") InputStream file) {
     ...
   }

Alternatively you can use the MIME multipart API, see the following
JavaDoc for more details:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/contribs/jersey-multipart/index.html

Specifically:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/contribs/jersey-multipart/com/sun/jersey/multipart/FormDataMultiPart.html


   @Consumes("multipart/form-data")
   @POST
   public void post(FormDataMultiPart formData) {
    FormDataPart p = formData.getField("file");
    InputStream file = p.getValueAs(InputStream.class);
     ...
   }

For either of the above you require a dependency on JavaMail:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/jersey/dependencies.html

You can reuse the MIME multipart API on the Jersey client side.

Paul.

> Robert
>
> 2009/2/24 Paul Sandoz <Paul.Sandoz_at_sun.com>:
>>
>> On Feb 24, 2009, at 2:08 PM, Robert Naczinski wrote:
>>
>>> Hi,
>>>
>>> it is possible with Jersey to receive a file from a request? If is
>>> so,
>>> has anybody a snippet?
>>>
>>
>> Do you mean the uploading of a file from say a browser using an
>> HTML form?
>>
>> Or do you mean a client directly sending the bytes of the file as a
>> request
>> entity and the server should reproduce a file that contains those
>> same bytes
>> on the server-side?
>>
>> Paul.
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>