10

I have read several similar questions on StackOverflow but haven't found a solution to this problem yet.

I am uploading a blob from Android to App Engine's Blobstore through an HTTPPost to the upload URL generated by the Blobstore service. I want to be able to send some textual metadata with this request that identifies this blob. I want to retrieve this information along with the blob key in the upload handler servlet that is called after the blob is uploaded.

The problem is that the blob is uploaded using multipart encoding and App Engine does not support the Servlet v3.0 standard, so I can't use req.getPart() to get the textual part. (The blob itself is returned by the Blobstore service, so that part of the request is already parsed for us.)

How can I get around this problem by passing just one text parameter along with the file that is uploaded to the Blobstore and retrieve it in the servlet that is called after the upload of the blob?

Thanks so much for your help! Quite stuck on this one!

Here is the code that I use for HttpPost on Android:

        File file = new File(filePath);
        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder
                .create();
        entityBuilder.addBinaryBody("file", file);           
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(blobUploadURL);
        httpPost.setEntity(entityBuilder.build());
        try {
            HttpResponse response = httpClient.execute(httpPost);
            statusCode = response.getStatusLine().getStatusCode();
        }

UPDATE (Dec 8, '14):

I added a text-body to the entity builder before building the multipart-entity for the HttpPost request as follows:

        String param="value";
        entityBuilder.addTextBody("param", param);

For the servlet that handles the Blobstore's callback after the blob has uploaded, I used the method described by Google to parse an HttpPost request on App Engine in this tutorial as given below:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {  

    String paramNames="default";

    try {
        ServletFileUpload upload=new ServletFileUpload();
        FileItemIterator iterator=upload.getItemIterator(req);
        while(iterator.hasNext()){
            FileItemStream item=iterator.next();
            InputStream stream=item.openStream();
            if(item.isFormField()){
                paramNames+=item.getFieldName() + ", ";
            }
        }
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        paramNames="error";
    }

    //save the paramNames variable in the Datastore for debugging later
    saveParamNamesInDatastore(paramNames);
}

However, when I check the paramNames variable in the datastore after this operation, its value remains "default". That means no form field was found in the rewritten POST request that Blobstore passed to the upload handler servlet. Where to go from here??

3 Answers 3

1
+50

There's a way to post meta data with your blob with the information that would not be represented by a blobkey:

  1. In your upload form, include this:

    method="post" enctype="multipart/form-data"
    
  2. Now you can either add hidden fields:

    <input type="hidden" name="myName" value="<%= myName %>"/>
    
  3. Or you can add input fields:

    <input type="text" name="myText" size="50" value="Enter your text"/>
    
  4. In your servlet, make sure the post handler reads the meta-data

    String userName = req.getParameter("myName");
    
  5. Now you have the Upload form with all the information.

  6. When you are passing the information to serve the blob, you can use

    &blobkey=daffedafdfe&myName=Blah
    

So, you are not exactly storing the information in the blob itself, but you can include it in the upload form.

6
  • Thanks for your suggestion! I believe the request.setParameter() does not work with multipart-form encoding. Can you confirm if it works for you? If yes, it would be very helpful to see the Java code for the upload request and the blob upload handler.
    – Price
    Commented Dec 14, 2014 at 18:57
  • Why do you need .setParameter on the request object? The multi-form should send a post to your handler that has all the fields (including the meta-data your form provided). So in your servlet's Post handler, just use .getParameter("parameterName") and extract the meta-data you want with the Blob.
    – Ying Li
    Commented Dec 14, 2014 at 19:04
  • Thanks! I will test the request.getParameter("paramName") method in the upload handler servlet, but I doubt it will work. Have you tried this before?
    – Price
    Commented Dec 14, 2014 at 19:08
  • Yeah, I have it in my production code :) Make sure the fields are passed on by your form though, if it's not user-input, then make sure a hidden field exists. As long as it's passed on with the Post, it will work on the servlet side.
    – Ying Li
    Commented Dec 14, 2014 at 19:11
  • Thanks so much! :) This works perfectly! I incorrectly presumed since req.setParameter() does not work with multipart-encoding, req.getParameter() will also fail. Although, I can confirm the method recommended by Google that I have mentioned in the question (the one using ServletFileUpload and FileInputStream) is not working.
    – Price
    Commented Dec 14, 2014 at 19:39
0

From what I can tell, the MultiPartEntityBuilder is respsonsible for mocking up an HTML "form" of enctype="multipart/form-data", and then by the time you get to the DefaultHttpClient lines, you are just sending the request from this form via POST. You should look into the documentaiton for the the entity builder. It has a function to add text fields to the "form"[1].

[1] - http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html#addTextBody(java.lang.String,%20java.lang.String)

1
  • Thanks! I followed your suggestion and have updated my question to reflect the results from this trial. Could you please take a quick look and suggest any next steps?
    – Price
    Commented Dec 8, 2014 at 6:50
0

Just in case, you might want to specify

entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

after creating the object.

If that's not working, it might be advisable to post against requestbin to see exactly what you are receiving

1
  • Thanks Javier! Setting the browser compatible mode did not help. Posting against requestbin confirmed the request contains the parameter "param" as a "form / post parameter".
    – Price
    Commented Dec 10, 2014 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.