我怎么可以在上传一个blob的Blob存储的POST请求提交的文本字段,并在上传处理程序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?

查看:720
本文介绍了我怎么可以在上传一个blob的Blob存储的POST请求提交的文本字段,并在上传处理程序BLOB找回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的StackOverflow几个类似的问题,但还没有找到一个解决这个问题呢。

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

我通过HTTPPost上传从Android的一个blob到App Engine的Blob存储由Blob存储服务生成的上传的URL。我希望能够把一些文本元数据与这一要求标识此斑点。我想沿着检索这些信息在上传处理程序的servlet的BLOB键被称为斑被上传后。

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.

的问题是,斑被使用多编码和应用程​​序引擎不支持Servlet 3.0标准的上传,所以我不能用req.getPart()来获取文字部分。 (blob的本身是由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. (The blob itself is returned by the Blobstore service, so that part of the request is already parsed for us.)

如何,我可以解决这个问题,通过与被上传到Blob存储,并在被BLOB的上传后调用这个servlet检索文件的传递只是一个文本参数?

非常感谢您的帮助!很停留在这一个!

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

下面是我使用HttpPost在Android上的code:

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();
        }

更新(12月8日,'14):

我添加一个文本体的实体建造者建造的多部分实体的HttpPost要求如下之前:

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);

有关的BLOB后处理Blob存储的回调servlet的上传,我用谷歌所描述的方法来解析的这个的教程,如下所示:

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);
}

然而,当我这样操作后检查数据存储中paramNames变量,它的值保持默认。这意味着,没有任何形式的现场被发现在Blob存储传递给上传处理程序的servlet的改写POST请求。凡何去何从?

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??

推荐答案

有一种方法来发布元数据与您的斑点与不会再由blobkey psented $ P $的信息:

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

  1. 在你上传的形式,包括这样的:

  1. In your upload form, include this:

method="post" enctype="multipart/form-data"

  • 现在你可以添加隐藏字段:

  • Now you can either add hidden fields:

    <input type="hidden" name="myName" value="<%= myName %>"/>
    

  • 或者你也可以添加输入字段:

  • Or you can add input fields:

    <input type="text" name="myText" size="50" value="Enter your text"/>
    

  • 在你的servlet,确保后处理程序读取的元数据。

  • In your servlet, make sure the post handler reads the meta-data

    String userName = req.getParameter("myName");
    

  • 现在你有所有信息上传表单。

  • Now you have the Upload form with all the information.

    当您传递的信息服务斑点,你可以使用

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

    &blobkey=daffedafdfe&myName=Blah
    

  • 那么,你是不是完全存储在BLOB本身的信息,但可以将其包含在上传表单。

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

    这篇关于我怎么可以在上传一个blob的Blob存储的POST请求提交的文本字段,并在上传处理程序BLOB找回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆