users@jersey.java.net

Re: [Jersey] Uploading files using Jersey client

From: Imran M Yousuf <imran_at_smartitengineering.com>
Date: Thu, 19 Mar 2009 16:20:03 +0600

Thanks a lot to both Paul and Naresh. But now I am stuck with a
problem from Jersey Client :).

I am using the following code on the server side -
    @Path("test")
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    public void testFileAttachment(
                                   @FormParam("file") final
InputStream fileStream,
                                   @FormParam("file") final
FormDataContentDisposition disposition) {
        //The problem is disposition does not have any information :(
        System.out.println(disposition.getFileName()); //null
    }

The client code is -
            InputStream imageStream =
                getClass().getClassLoader().getResourceAsStream(
                "Splash-resized.jpg");
            MediaType imageType = new MediaType("image", "jpeg");
            FormDataMultiPart part = new FormDataMultiPart();
            FormDataMultiPart bodyPart = part.field("file",
imageStream, imageType);
            bodyPart.getHeaders().putSingle("Content-Type",
imageType.toString());
            bodyPart.getHeaders().putSingle("Content-Disposition",
"form-data; filename=userfile; size=983");
            resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(
                part);

I am pretty sure I am doing some stupidly wrong but I am not sure what
and where :(. Can someone please guide me in correcting it?

Also I would actually like if the following code would automatically
add headers for content -
            File file = new File("src/test/resources/Splash-resized.jpg");
            MediaType imageType = new MediaType("image", "jpeg");
            FormDataMultiPart part = new FormDataMultiPart();
            FormDataMultiPart bodyPart = part.field("file", file, imageType);
            resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(
                part);

Do you think its worth a change?

Thank you,

Imran

On Thu, Mar 19, 2009 at 3:43 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>
> On Mar 19, 2009, at 10:41 AM, Naresh wrote:
>
>> Paul Sandoz wrote:
>>>
>>> HI Naresh, Imran,
>>>
>>> The media type "application/x-www-form-urlencoded" is the wrong media
>>> type to use for uploading files.
>>
>> My apologies...
>> I thought it works with "application/x-www-form-urlencoded" too.
>> Thanks for correcting me Paul.
>>
>
> No apology required :-) The following section of the HTML spec is worth
> reading:
>
>  http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
>
> Paul.
>
>> -Naresh
>>>
>>> The media type "application/form-data" needs to be used.
>>>
>>> See the following email for an attachment that contains a simple maven
>>> project using forms:
>>>
>>>
>>>
>>> http://markmail.org/search/?q=list%3Anet.java.dev.jersey.users+form-data#query:list%3Anet.java.dev.jersey.users%20form-data%20order%3Adate-backward+page:1+mid:qbkax55u7crqu7wi+state:results
>>>
>>>
>>> I think we need a good sample showing the use of various forms. Hint
>>> hint to any one who wants to contribute one :-)
>>>
>>>
>>>
>>> Note that i am in the process of fixing a number of bugs and improving
>>> form-data processing. I will deprecate the use of @FormParam with
>>> "multipart/form-data". Instead developers will be encouraged to use
>>> @FormDataParam which will not overload the semantics of @FormParam and
>>> will i think cause less confusion, and plus is a place i can clear
>>> document behavior for multipart/form-data support.
>>>
>>> Paul.
>>>
>>> On Mar 19, 2009, at 10:23 AM, Naresh wrote:
>>>
>>>> Imran M Yousuf wrote:
>>>>>
>>>>> On Thu, Mar 19, 2009 at 12:17 PM, Naresh <bsnaresh_at_sun.com> wrote:
>>>>>
>>>>>> Hi Imran,
>>>>>>
>>>>>> please check the [simple-servlet] sample. It has the code sample for
>>>>>> submitting a form.
>>>>>>
>>>>>> [simple-servlet]
>>>>>>
>>>>>> http://download.java.net/maven/2/com/sun/jersey/samples/simple-servlet/1.0.3-SNAPSHOT/simple-servlet-1.0.3-SNAPSHOT-project.zip
>>>>>>
>>>>>>
>>>>>
>>>>> Thanks. Any idea how I could add an attachment to the Form?
>>>>>
>>>> You can use the HTML file control for uploading files, and @FormParam
>>>> annotation for accessing the file.
>>>> The following is a sample code:
>>>>
>>>> @POST
>>>> @Path("form")
>>>> @Produces("text/plain")
>>>> @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
>>>> public String getFileContents(@FormParam ("inputfile") InputStream
>>>> fileStream) {
>>>> ....
>>>> }
>>>>
>>>> HTH,
>>>> Naresh
>>>>>
>>>>>> Thanks,
>>>>>> Naresh
>>>>>>
>>>>>> Imran M Yousuf wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> Can someone please help me find code sample for submitting a form
>>>>>>> (preferably includes a file attachment) using Jersey Client API?
>>>>>>>
>>>>>>> Thank you,
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> 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
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>



-- 
Imran M Yousuf
Entrepreneur & Software Engineer
Smart IT Engineering
Dhaka, Bangladesh
Email: imran_at_smartitengineering.com
Blog: http://imyousuf-tech.blogs.smartitengineering.com/
Mobile: +880-1711402557