users@jersey.java.net

Re: [Jersey] Java equivalent of curl -F

From: Andrew Hopkinson <Andrew.Hopkinson_at_Sun.COM>
Date: Tue, 13 Oct 2009 11:47:28 +0100
Paul,

Thanks for that you second example was just what I needed.

Cheers

Andrew

Paul Sandoz wrote:
Hi Andrew,

There are three ways, each of a higher level (i hope each explanation helps to progressively explain the API).

Try using FormDataMultiPart:

  https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/contribs/jersey-multipart/com/sun/jersey/multipart/FormDataMultiPart.html

  InputStream in = ... // get input stream for the file name
  FormDataMultiPart fdmp = new FormDataMultiPart().field(
      "contents",
      in,
      MediaType.APPLICATION_OCTET_STREAM_TYPE);

  ClientResponse cr = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class, fdmp).


If you need to set further information beyond the identification of the body part ("contents" in this example) you can add a FormDataBodyPart instance:

  https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/contribs/jersey-multipart/com/sun/jersey/multipart/FormDataBodyPart.html

using a built instance of FormDataContentDisposition:

  https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/jersey/com/sun/jersey/core/header/FormDataContentDisposition.html

For example:

  InputStream in = ... // get input stream for the file name
  FormDataMultiPart fdmp = new FormDataMultiPart();
  FormDataBodyPart fdp = new FormDataBodyPart(
      FormDataContentDispositionBuilder.name("contents").fileName(...).creationDate(...).build(),
      in,
      MediaType.APPLICATION_OCTET_STREAM_TYPE);
    );
  fdmp.bodyPart(fdp);


Finally, Jersey also provides the helper class FileDataBodyPart:

  https://jersey.dev.java.net/nonav/apidocs/1.1.2-ea/contribs/jersey-multipart/com/sun/jersey/multipart/file/FileDataBodyPart.html#FileDataBodyPart%28%29

  File f = ... / get the file
  FormDataMultiPart fdmp = new FormDataMultiPart();
  fdmp.bodyPart(new FileDataBodyPart("contents", f, MediaType.APPLICATION_OCTET_STREAM_TYPE));

Paul.



On Oct 13, 2009, at 11:32 AM, Andrew Hopkinson wrote:

Hi,

I have been trying, unsuccessfully, to build a simple class that executes the equivalent of the following:

curl -F "contents=@<yourFilename>" -X POST http://localhost:8080/spaces/rest/store/space/<yourSpaceName>/artifact

I have tried a number of options based around MultivalueMaps and MulitPart with no luck. I have tried the following  and permutations of it but am still stuck so if anyone has any suggestion I would appreciate any hints.


          String filename = args[0].replaceAll("\\\\", "/");
          int pos = filename.lastIndexOf("/");
          MultivaluedMap formData = new MultivaluedMapImpl();
//            formData.add("name", "contents");
          formData.add("filename", filename.substring(pos+1));

          file = new File(args[0]);
          fis = new FileInputStream(file);
          byte[] fileBytes = new byte[(int) file.length()];
          fis.read(fileBytes);
//            System.out.println("File Bytes : " + new String(fileBytes));
                    multiPart = new MultiPart().
                  bodyPart(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE).
                  bodyPart(fileBytes, MediaType.APPLICATION_OCTET_STREAM_TYPE);

          System.out.println("MultiPart = " + multiPart.getHeaders());

          response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.WILDCARD_TYPE).post(ClientResponse.class, multiPart);
//            response = webResource.type("multipart/mixed").post(ClientResponse.class, multiPart);
          System.out.println("Response = " + response);

Thanks

Andrew




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@jersey.dev.java.net
For additional commands, e-mail: users-help@jersey.dev.java.net



--
Andrew Hopkinson

Principal Engineer / Enterprise Integration Architect
Field Assist Support Team


Sun Microsystems, Inc.
Java House, Guillemont Park, Minley Road, Blackwater
Camberley, Hampshire GU17 9QG GB
Phone : +44 (0)1252 422 890
Mobile : +44 (0)7876 682 529

+44 (0)7884 185 063
Fax : +44 (0)1252 423 530
Email : Andrew.Hopkinson@Sun.COM
Blog : http://blogs.sun.com/toxophily/

GlassFish ESB
Sun's Open ESB Community (http://open-esb.dev.java.net)