简体   繁体   English

如何在POST请求中提交一个文本字段,将blob上传到Blobstore并在blob的上传处理程序中检索它?

[英]How can I submit a text field in the POST request that uploads a blob to the Blobstore and retrieve it in the upload handler for the blob?

I have read several similar questions on StackOverflow but haven't found a solution to this problem yet. 我已经在StackOverflow上阅读了几个类似的问题,但还没有找到解决这个问题的方法。

I am uploading a blob from Android to App Engine's Blobstore through an HTTPPost to the upload URL generated by the Blobstore service. 我正在通过HTTPPost将一个blob从Android上传到App Engine的Blobstore,再到Blobstore服务生成的上传URL。 I want to be able to send some textual metadata with this request that identifies this blob. 我希望能够使用此请求发送一些文本元数据来标识此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. 我想在上传blob后调用的上传处理程序servlet中检索此信息以及blob键。

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. 问题是使用多部分编码上传blob并且App Engine不支持Servlet v3.0标准,因此我无法使用req.getPart()来获取文本部分。 (The blob itself is returned by the Blobstore service, so that part of the request is already parsed for us.) (blob本身由Blobstore服务返回,因此已经为我们解析了部分请求。)

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? 如何通过传递一个文本参数以及上传到Blobstore的文件并在上传blob后调用的servlet中检索它来解决此问题?

Thanks so much for your help! 非常感谢你的帮助! Quite stuck on this one! 相当坚持这一个!

Here is the code that I use for HttpPost on Android: 以下是我在Android上用于HttpPost的代码:

        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): 更新(2014年12月8日):

I added a text-body to the entity builder before building the multipart-entity for the HttpPost request as follows: 在为HttpPost请求构建multipart-entity之前,我在实体构建器中添加了一个text-body,如下所示:

        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: 对于在blob上传后处理Blobstore回调的servlet,我使用Google描述的方法在教程中解析App Engine上的HttpPost请求,如下所示:

@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". 但是,在此操作之后检查数据存储区中的paramNames变量时,其值仍为“default”。 That means no form field was found in the rewritten POST request that Blobstore passed to the upload handler servlet. 这意味着在Blobstore传递给上传处理程序servlet的重写POST请求中找不到表单字段。 Where to go from here?? 从这往哪儿走??

There's a way to post meta data with your blob with the information that would not be represented by a blobkey: 有一种方法可以使用blob发布带有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 在servlet中,确保post处理程序读取元数据

     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 当您传递信息以服务blob时,您可以使用

     &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. 因此,您并不是完全将信息存储在blob本身中,而是可以将其包含在上载表单中。

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. 据我所知,MultiPartEntityBuilder可以用来模拟enctype =“multipart / form-data”的HTML“表单”,然后当你到达DefaultHttpClient行时,你只是从这个表单发送请求通过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]添加文本字段的功能。

[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] - http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntityBuilder.html#addTextBody(java.lang.String,%20java.lang 。串)

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 如果这不起作用,最好发帖反对requestbin ,看看你收到的确切内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM