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