If i Upload a file to my servlet like this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write");
try
{
MultipartEntity entity = new MultipartEntity();
entity.addPart("type", new StringBody("photo"));
entity.addPart("data", new FileBody(image));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
}
catch (ClientProtocolException e) {}
catch (IOException e) {}
How can I retrieve the content at the servlet?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
{
request.???
}
- I’m using Google App Server as my Servlet API
If the version of your Servlet Container or Server or Engine < 3.0 (like 2.5 or earlier) , you may want to take advantage of the third-party Library Apache Commons FileUpload. Although the file implied an use for uploaded Files, it also deals effectually with uploaded posted Data from POST-Methods like it explained here.
the Servlet API, from the version 3.0 offers some calls in oder to deal with posted Data, with was sent within a POST-Request. the only requirement is that the MIME-Type encoding of your entity content is “multipart/form-data“.
then your can retrieve each “part” of your content using either:
getPart(String partName): where “partName” is the name of a part of your Multicontent entity.
getParts():
it achieves the same results as getPart(partName), whereas the given data here a collection of all part of the sent data. to retrieve each par of the Part of this collection, just use thread-safe iteration over the collection:
Because the getPart()/getParts() only works beginning at the Servlet 3.0 version, you’ll make sure to use the supporting Servlet container and/or upgrade your current Servlet Container. some Server or Servlet container that supports 3.0: