简体   繁体   English

一步上传,使用Google App Engine上传图片

[英]A one step upload for uploading images using google app engine

I am trying to implement a very basic functionality of uploading images from Android,iPhone and web clients to the google app engine. 我正在尝试实现一个非常基本的功能,即将图像从Android,iPhone和Web客户端上载到Google App引擎。 I did an initial version of the implementation thanks to this blog: 感谢博客,我做了实施的初始版本:

However there always seems to be a 2 step process to uploading an image: 但是,上传图片似乎总是需要两个步骤:

  1. Get the initial upload URL to POST to using the createUploadUrl(). 使用createUploadUrl()获取初始上传URL,以发布到POST。 I am attaching the fragment of code which I use : 我附上我使用的代码片段:
public class CreateUploadUrl extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
        String uploadURL = blobstoreService.createUploadUrl("/image/uploadImage");
        resp.setContentType("text/plain");
        resp.getWriter().println(uploadURL);
    }
}
  1. POST the image using the URL which you just "got" 使用您刚刚“获得”的网址发布图片
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        BlobKey blobKey = ParameterExtractor.getBlobParameter(req, "blob-key", blobstoreService);
        if (blobKey == null) {
            log.info("blob Id is null. POST failed");
        } else {
            log.info("ze business logic");
        }
    }

My question is if it is possible to do it in one step since right now all clients need to do a http GET to get the upload URL and then a http POST to POST the image. 我的问题是,既然现在所有客户端都需要执行http GET来获取上载URL,然后执行http POST来发布图像,那么是否可以一步完成呢。

Is it not possible to just do one Http POST with a predefined URL. 是否只能使用预定义的URL执行一次Http POST。

Thanks Rajat 谢谢拉贾特

This is possible, with limitations. 这是有限制的。 You can bypass the UploadUrl mechanism by creating blobs directly in your servlet using the (currently experimental) createNewBlobFile API. 您可以使用(当前处于实验阶段) createNewBlobFile API在servlet中直接创建blob,从而绕过UploadUrl机制。 In your mobile app(s) create an HTTP request encoded as multipart/form-data, and teach your servlet how to decode such a thing (consult eg How to upload files in JSP/Servlet? ). 在您的移动应用程序中,创建一个编码为multipart / form-data的HTTP请求,并教您的Servlet如何解码此类内容(例如, 如何在JSP / Servlet中上传文件? )。 Be aware that HTTP requests are limited to 32MB ; 请注意,HTTP请求限制为32MB with form encoding the amount of binary data you can upload will be less than that. 使用格式编码后,您可以上传的二进制数据量将更少。

Sure you can do it with single POST. 当然,您可以使用单个POST进行操作。 For example you have user that have an id. 例如,您有一个具有ID的用户。 This user select image and you send in POST image data and user data on client side. 该用户选择图像,然后您在客户端发送POST图像数据和用户数据。

On server side (GAE) you have url for image uploding (your_host/imageUpload) and server or Spring controller that read data from request and write it to Blobstore. 在服务器端(GAE),您具有用于图像上移的URL(your_host / imageUpload)以及用于从请求中读取数据并将其写入Blobstore的服务器或Spring控制器。

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

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