Issue
I am building a servlet that accepts an image in a POST request. with each post there should be an associated ID. My question is how to pass these two distinct data values in a post, where one is a short string, and another is a large chunk of binary data.
I could have both as post parameters,
id=123
content=...megabytes of binary data...
but I need the flexibility of handling the content as a stream, as it could be quite large. I could also follow the above pattern by parsing the input myself as binary data, which I'd like to avoid. I guess I'd need to parse it character by character looking for the keys. ugly.
Am I missing the correct pattern for handling this?
Solution
The standard technique, used in browser to send form data containing both text and file inputs, is to use multi-part form data.
Apache commons FileUpdload could be used at server-side to parse the request, and give you access to the uploaded image as a stream.
Answered By - JB Nizet
Answer Checked By - Pedro (JavaFixing Volunteer)