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
>>
>