My server receives multipart form data from a mobile app. The result of printing out the request using request.content.toString(UTF_8) is shown below. The image is sent by the mobile as a base64-encoded string. How do I save the image data to file?
--*****org.apache.cordova.formBoundary
Content-Disposition: form-data; name="value1"
Furniture
--*****org.apache.cordova.formBoundary
Content-Disposition: form-data; name="value2"
Chair
--*****org.apache.cordova.formBoundary
Content-Disposition: form-data; name="file"; filename="image.jpg"
Content-Type: image/jpeg
����JFIF��XExifMM*�i...rest of image goes here...
--*****org.apache.cordova.formBoundary--
I am yet to parse the multipart form data, but saving the image turned out to be easy (once you know how). To test, I’m uploading only the image during testing using:
I’m using Finagle, which depends on the netty libraries, so the request.content() returns a object of type org.jboss.netty.buffer.ChannelBuffer, for which you extract the image data as follows:
To save the file from the request content to file:
To pass the request content on to a web service expecting base64binary: