users@jersey.java.net

Uploading files from the Client using the MultiPart API

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Mon, 21 Sep 2009 15:12:03 -0600

Hello,

I was trying to upload files using the FileDataBodyPart on the client
side to a Multipart resource.

Here is the client code:

MultiPart multiPart = new MultiPart();
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("name", new
Fle(fileEntity), MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
ClientResponse response = resource.type(new MediaType("multipart",
"mixed")).post(ClientResponse.class, multiPart);

The resource method looks like:
    @POST
    @Consumes("multipart/mixed")
    public Response multipart(MultiPart multiPart) throws IOException {
        BodyPartEntity bpe = (BodyPartEntity)
multiPart.getBodyParts().get(0).getEntity();
        StringBuilder sb = new StringBuilder();
        InputStream stream = bpe.getInputStream();
        InputStreamReader reader = new InputStreamReader(stream);
        char[] buffer = new char[2048];
        while (true) {
            int n = reader.read(buffer);
            if (n < 0) {
                break;
            }
            sb.append(buffer, 0, n);
        }
        return Response.ok("SUCCESS").build();
    }

I get 415 (UNSUPPORTED_MEDIA_TYPE) when doing POST. I am not sure what I
am missing here? Is there an example which uses FileDataBodyPart (file
upload using the client API)?

Thank you,
Arul