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
>