users@jersey.java.net

Multipart Post

From: Alexander Birmingham <alexx1523_at_gmail.com>
Date: Mon, 4 Jan 2010 16:31:43 -0800

Hi Everyone:

I've been trying to do a multipart post using the Jersey Client, and it's
just about near driving me crazy. The examples I've found have been helpful
in modelling my code, but haven't taken me quite to the end.

*I am looking to duplicate the following curl command:*

curl -F "file=_at_test.zip;type=application/octet-stream" -F "e=
alexx1523_at_gmail.com" -F "l=iQdad" -F "t=5Cp" -F "client_id=1000076"
http://my.url.here.com/home/upload

*This is my java code:*

        MultivaluedMap<String, String> queryParams;
        File file = new File("/tmp/test.zip");

        addArgument(SessionUser.getCURRENTUSER()); // This adds the
parameters you see above to queryParams

        MultiPart multiPart = new MultiPart().bodyPart(new
FileDataBodyPart("file", file, MediaType.APPLICATION_OCTET_STREAM_TYPE));


webResource.queryParams(queryParams).type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
multiPart);

The response indicates that the post is missing field "file". I am unable to
determine if this is because the binary is not being attached correctly, or
if the attachment is not being sent with the correct name. The name has to
be "file", as occurs in the above curl, or it will not be recognized.

Please help!

Best Regards,
Alexander Birmingham