users@jersey.java.net

Re: [Jersey] How to use multipart/form-data

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 18 Jun 2008 14:59:35 +0200

Hi Max,

I have added experimental support for "multipart/form-data" using
@FormParam.

For this to work the resource method needs to have a
@ConsumeMime("multipart/form-param") associated with it.

Underneath the covers the message body readers will be used, so you can
use String, JAXB beans etc. The rules are the same as request entities,
and not the same rules as for other parameters.

In your example you can now do this:

   @POST
   @ConsumeMime("multipart/form-data")
   public Response createGraphic (
       @FormParam( "field1" ) String name,
       @FormParam( "pics" ) InputStream image ) {
     ...
   }


If you wrote your own image-based message body reader then you could do:

   @POST
   @ConsumeMime("multipart/form-data")
   public Response createGraphic (
       @FormParam( "field1" ) String name,
       @FormParam( "pics" ) Image image ) {
     ...
   }


If you had a method:

   @POST
   @ConsumeMime({"multipart/form-data",
       "application/x-www-form-urlencoded"})
   public Response createGraphic (
       @FormParam( "field1" ) String name,
       @FormParam( "pics" ) Image image ) {
     ...
   }

then the rules using message body readers would apply for request sent
using "application/x-www-form-urlencoded", but it is assumed that the
media type of such form parameters is "text/plain" so you may not get
the same behaviour for when "application/x-www-form-urlencoded" only is
declared.

There is probably ways to clarify on the use of both media types for one
method, but i think it is hard to fully unify and i would not recommend
it and instead manage such stuff distinctly.

Currently the implementation is not very smart, and will likely result
in the buffering of the complete message. If the declared form
parameters are in the same order as they occur in the message then we
should supporting streaming.

Paul.

Paul Sandoz wrote:
> Hi Max,
>
> Currently @FormParam only works correctly for the media type:
>
> application/x-www-form-urlencoded
>
> For the moment you need to use the JavaMail API and the type:
>
> javax.mail.internet.MimeMultipart
>
> which i fully admit is not as nice as you would like.
>
> I think it possible to support "multipart/form-data" by utilizing the
> JavaMail API under the covers, just a matter of getting the time to
> program it... (Plus i don't want to introduce a direct dependency on the
> JavaMail jars.)
>
> Paul.
>
> Max Scheffler wrote:
>> Hi,
>>
>> i want to create an image resource by uploading an image and setting
>> some informations for this image.
>>
>> The following is a possible POST body:
>>
>> Content-type: multipart/form-data, boundary=AaB03x
>>
>> --AaB03x
>> content-disposition: form-data; name="field1"
>>
>> Joe Blow
>> --AaB03x
>> content-disposition: form-data; name="pics"; filename="file1.txt"
>> Content-Type: text/plain
>>
>> ... contents of file1.txt ...
>> --AaB03x--
>>
>> I tried to use the following code:
>>
>> @POST
>> public Response createGraphic ( @FormParam( "field1" ) String name,
>> @FormParam( "pics" ) Object object ) {
>> ...
>> }
>>
>> But this approach didn't work fine.
>>
>> How can I fetch the parameters (in this example: field1) and the file?
>>
>> Greetings
>> Max
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109