users@jersey.java.net

[Jersey] Re: How to upload file in FormDataMultiPart

From: Michal Gajdos <michal.gajdos_at_oracle.com>
Date: Wed, 13 Jun 2012 18:20:15 +0200

Hi Brendan,

are you able to send a simple post request or even a simple multipart
request without uploading a file, i.e.

MultiPart entity = new MultiPart().bodyPart("This is the only segment",
new MediaType("text", "plain"));

to your server using Jersey client without problems?

thanks,
Michal


On 13.6.2012 15:45, Brendan cheng wrote:
> Hi Michal,
> This Curl works for me:
> curl --header 'accept:application/json' --form "thumbnail=@/Users/ccp999/Desktop/bond.jpg" --form "thumbnail=@/Users/ccp999/Desktop/22m.png" http://192.168.33.156:8000/uploadTagFile/447
> is this has different effect from yours?
> I added logging in my testing too and result:
>
> Jun 13, 2012 9:34:07 PM com.sun.jersey.api.client.filter.LoggingFilter log
> INFO: 1 * Client out-bound request
> 1 > POST https://192.168.33.156:8000/itags/uploadTagFile/447
> 1 > Content-Type: multipart/form-data
> 1 > Accept: application/json
> --Boundary_1_1521935075_1339594447782
> Content-Type: image/png
> Content-Disposition: form-data; filename="22.png"; modification-date="Sat, 09 Jun 2012 14:30:06 GMT"; size=19813; name="thumbnail"
> it looks fine, right? is the sequence make the different?
> Brendan
> ----------------------------------------
>> Date: Wed, 13 Jun 2012 13:59:23 +0200
>> From: michal.gajdos_at_oracle.com
>> To: users_at_jersey.java.net
>> Subject: [Jersey] Re: How to upload file in FormDataMultiPart
>>
>> Hi,
>>
>> your example seems valid (assuming that in the real code you're creating
>> a web resource with an absolute URI). Can you try to add a LoggingFilter
>> into your client
>>
>> c.addFilter(new LoggingFilter());
>>
>> and take a look into the log? In your case you should see something like:
>>
>> 13.6.2012 13:36:18 com.sun.jersey.api.client.filter.LoggingFilter log
>> INFO: 1 * Client out-bound request
>> 1 > POST http://localhost/upload
>> 1 > Content-Type: multipart/form-data
>> 1 > Accept: application/json
>> --Boundary_1_1048095360_1339587378746
>> Content-Type: image/png
>> Content-Disposition: form-data; filename="<filename>";
>> modification-date="Wed, 13 Jun 2012 10:33:32 GMT"; size=1000;
>> name="thumbnail"
>>
>> <data>
>> --Boundary_1_1048095360_1339587378746--
>>
>> where `name="thumbnail"` indicates the control name you set to the
>> FileDataBodyPart object (new FileDataBodyPart("thumbnail", file)) and
>> `filename="<filename>"` should contain the filename of a file you're
>> trying to upload.
>> So on the client side the filename should not be replaced by the control
>> name and vice versa.
>>
>> Also, does the following `curl` command work for you as expected?
>>
>> curl -X POST -H "Content-Type: multipart/form-data" -H "Accept:
>> application/json" -F "thumbnail=@<filename>" http://127.0.0.1/upload
>>
>> (replace the <filename> placeholder with the actual file name and run
>> this command in the directory where <filename> is present)
>>
>> regards,
>> Michal
>>
>> On 12.6.2012 5:06, Brendan cheng wrote:
>>> Hi,
>>> I solved half the problem by using FileDataBodyPart:
>>> FormDataMultiPart form = new FormDataMultiPart();
>>> form.bodyPart(new FileDataBodyPart("thumbnail", file));
>>> String s = c.resource("127.0.0.1").path("upload")
>>> .type(MediaType.MULTIPART_FORM_DATA)
>>> .accept(MediaType.APPLICATION_JSON).post(String.class, form);
>>> But I found that the control name has no effect: at the end the filename is always become the control name which my server still cannot detect the file.
>>> is this the bug for the FileDataBodyPart?
>>> Brendan
>>> ----------------------------------------
>>>> From: ccp999_at_hotmail.com
>>>> To: users_at_jersey.java.net
>>>> Date: Tue, 12 Jun 2012 01:59:22 +0000
>>>> Subject: [Jersey] How to upload file in FormDataMultiPart
>>>>
>>>>
>>>> Hi,
>>>> I need to code a java client with jersey to upload files to a server written in javascript.
>>>> I can do this in webpage:
>>>> '<html>' +'<head>' +'<meta http-equiv="Content-Type" ' + 'content="text/html; charset=UTF-8" />' +'</head>' +'<body>' +'<form action="/upload" enctype="multipart/form-data" ' + 'method="post">' +'<input type="file" name="thumbnail" multiple="multiple">' +'<input type="submit" value="Upload file" />' +'</form>' +'</body>' +'</html>';
>>>> and I tried in Jersey as:
>>>> java.io.File file;
>>>> com.sun.jersey.api.client.Client c = com.sun.jersey.api.client.Client.create(config);
>>>> c.addFilter(new ConnectionListenerFilter(new ListenerFactory()));
>>>>
>>>> String s = c.resource("127.0.0.1").path("upload")
>>>> .type(MediaType.MULTIPART_FORM_DATA)
>>>> .accept(MediaType.APPLICATION_JSON).post(String.class, new FormDataMultiPart().field("thumbnail", file, MediaType.MULTIPART_FORM_DATA_TYPE));
>>>> The java client did upload file to server and it put the file in Body. it sounds like ok but actually the server side didn't pick up it as file and automatically store it in directory. which I compared the difference between javascript version and jersey, I found that Javascript will put the file into file attachment while jersey put it in body part. so the server store the body part in memory, not put it in directory.
>>>> Brendan
>>>>
>