users@jersey.java.net

Re: [Jersey] Java equivalent of curl -F

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 13 Oct 2009 12:25:52 +0200

On Oct 13, 2009, at 11:59 AM, Felipe Gaścho wrote:

> http://kenai.com/projects/puj/sources/arena/content/arena-client/src/test/java/com/kenai/puj/arena/client/test/UserManagementTestCase.java
>
> public int registerNewUser() {
> Form formData = new Form();
> formData.add(RegistrationConstants.NAME.value(), "Fake J. Unit");
> formData.add(RegistrationConstants.PASSWORD.value(), "fake");
> formData.add(RegistrationConstants.LOGIN.value(), "fake");
> formData.add(RegistrationConstants.EMAIL.value(),
> "fgaucho_at_gmail.com");
> formData.add(RegistrationConstants.CONFIRMATION_URL.value(),
> "http://localhost:8080/arena-dwr/confirm");
> ClientResponse response = arena.path("user").type(
> "application/x-www-form-urlencoded").post(ClientResponse.class,
> formData);
>
> return response.getStatus();
> }

That is for application/x-www-form-urlencoded. Andrew needs to use
multipart/form-data, which is what curl -F option utilizes for the
transmission of file contents.

The former media type is eminently suitable for the properties (as in
your case) but is not suitable for the upload of one or more binary
files.

Paul.

>
>
> On Tue, Oct 13, 2009 at 11:32 AM, Andrew Hopkinson
> <Andrew.Hopkinson_at_sun.com> 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_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
>
>
> --
> Looking for a client application for this service:
> http://fgaucho.dyndns.org:8080/arena-http/wadl
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>