1

I am looking to create a RESTful API for use with an Android and iOS app. So far I have been experimenting with using Jersey on the server and then the appropriate http libraries on the client side. At the moment I have been using multipart/related as the mimetype for the request with JSON forming the first part of the body then a jpeg image as the second.

So far I have had problems with making the request to the server, getting a 406 Not Acceptable from Jersey. I note that multipart/related is primarily used in sending emails. Is there actually a way that I can support mixed type content as an upload or have I entirely mis-understood the usage of multipart/related in this context?

marcus.ramsden
  • 2,633
  • 1
  • 22
  • 33

2 Answers2

2

You may want to look at this blog, for more information, but here is the important part to help you along:

http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
    @FormDataParam("file") InputStream uploadedInputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail) {

    String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;

    return Response.status(200).entity(output).build();
}

I expect you want multipart/form-data instead, as this is part of the description of multipart/related:

The Multipart/Related media type is intended for compound objects
consisting of several inter-related body parts. For a
Multipart/Related object, proper display cannot be achieved by
individually displaying the constituent body parts. The content-type of the Multipart/Related object is specified by the type parameter.
The "start" parameter, if given, points, via a content-ID, to the
body part that contains the object root. The default root is the
first body part within the Multipart/Related body.

For more on this mime type you can look at

https://www.rfc-editor.org/rfc/rfc2387

Community
  • 1
  • 1
James Black
  • 41,583
  • 10
  • 86
  • 166
  • Thanks, this one set me on the straight and narrow. I'll look to post this as a form to the API with the JSON string for the object in one field and the file in another. – marcus.ramsden Aug 20 '12 at 09:55
0

If you are wanting to submit image along with the json body, you can base64 encode the image and include the base64 string in the json. Then on the server side, you base64 decode the string and upload the image file to the blobstore. See the file upload example (at the bottom of the page) here https://developers.google.com/appengine/docs/java/blobstore/overview

Alternatively, you could do a separate upload to the blobstore and get the blobkey for the uploaded image. You can then include the blobkey in the json body that you post to the server.Using this approach you would need to get the uploadurl every time you need to do a new image upload.

Rob Curtis
  • 2,245
  • 24
  • 33
  • can you please share some example of how to do this for GAE upload. – Ankur Jain Feb 18 '13 at 19:42
  • @AnkurJain, do you mean using the blobstore with base64 encoded blob or do you mean using the blobstore to get the blob key? – Rob Curtis Feb 19 '13 at 04:02
  • Sir, I need any example of uploading the file as Base64 encoded Blob in GAE Blobstore. – Ankur Jain Feb 19 '13 at 05:11
  • For java decoding base64: http://stackoverflow.com/questions/469695/decode-base64-data-in-java/8381015#8381015 and writing to blobstore: https://developers.google.com/appengine/docs/java/blobstore/overview#Writing_Files_to_the_Blobstore . If you need more information ask a question. – Rob Curtis Feb 19 '13 at 05:28