4

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
Rami
  • 2,098
  • 5
  • 25
  • 37
  • 2
    Just search Stackoverflow - hundreds of questions/answers covering file upload + servlets do already exist! – home May 17 '12 at 10:26
  • Read http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/ – Hardik Mishra May 17 '12 at 10:30
  • @home The first step is searching the web and this site, and you should assume the one that's asking here already done that. I think that sending me to search by my self is not the spirit of this site. so at least you should have post any link instead just comment go search. – Rami May 17 '12 at 10:47

1 Answers1

4

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:

  1. getPart(String partName): where "partName" is the name of a part of your Multicontent entity.

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws  IOException 
    {
        String partName = "type"; // or "data"
        Part part = request.getPart(partName);
    
        // read your StringBody type
        BufferedReader reader = new BufferedReader( new InputStreamReader(part.getInputStream()));
        String line ="";
    
        while((line=reader.readLine())!=null)
        {
            // do Something with a line
            System.out.println(line);
    
        }
    
        // or with a binary Data
        partName="data";
        part = request.getPart(partName);
    
        // read your FileBody data
        InputStream is = part.getInputStream();
        // do Something with you byte data
    
        is.read();
        // is.read(b);
        // ..
    
    }
    
  2. 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:

Iterator<Part> iterator = request.getParts().iterator();
       Part parts = null;
       while (iterator.hasNext()) {
       parts = (Part) iterator.next();
          //rest of the code block removed
       }
    }

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:

  1. tomcat 7.0:
  2. Jboss Web
  3. Resin
Community
  • 1
  • 1
arthur
  • 3,245
  • 4
  • 25
  • 34
  • I'm using Google App Engine so I don't know If i can use Servlet 3.0 or how, but i'll try to find for details about it. thanks. – Rami May 17 '12 at 11:03
  • GAE Servlet API version is 2.5 so I guess I won't be able to use this. But thanks. – Rami May 17 '12 at 11:21
  • @Rami: i edited my answer according to your problem. if you read again, you'll find a link to quick presentation to a third party library "Apache Commons FileUpload" dealing with your situation. enjoy!! – arthur May 17 '12 at 11:23