Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE

后端 未结 3 957
一个人的身影
一个人的身影 2020-12-09 12:08

In my Android application, I want to upload image to the Blobstore, then retrieve an Upload url and the image\'s Blobkey, so I can store the Blobkey in the DataStore.

3条回答
  •  臣服心动
    2020-12-09 12:40

    After many tries i solved this problem. To store image in blobstore, first android needs to make request to servlet which will generate upload url :

    Android client : It will request to generate url and gets url from servlet

    HttpClient httpClient = new DefaultHttpClient();    
    //This will invoke "ImgUpload servlet           
    HttpGet httpGet = new HttpGet("my-app-url/ImgUpload"); 
    HttpResponse response = httpClient.execute(httpGet);
    HttpEntity urlEntity = response.getEntity();
    InputStream in = urlEntity.getContent();
    String str = "";
    while (true) {
        int ch = in.read();
        if (ch == -1)
            break;
        str += (char) ch;
    }
    

    ImgUpload.java - Servlet to generate url and sends response to client

    BlobstoreService blobstoreService = BlobstoreServiceFactory
                .getBlobstoreService();
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    
    //"uploaded" is another servlet which will send UploadUrl and blobkey to android client
    String blobUploadUrl = blobstoreService.createUploadUrl("/uploaded"); 
    
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setContentType("text/plain");
    
            PrintWriter out = resp.getWriter();
            out.print(blobUploadUrl);
        }
    

    In android client,write below code upload image to returned response from above servlet.

    //Save image to generated url
    HttpPost httppost = new HttpPost(str);
    File f = new File(imagePath);
    FileBody fileBody = new FileBody(f);
    MultipartEntity reqEntity = new MultipartEntity();
    reqEntity.addPart("file", fileBody);
    httppost.setEntity(reqEntity);
    response = httpClient.execute(httppost); //Here "uploaded" servlet is automatically       invoked
    urlEntity = response.getEntity(); //Response will be returned by "uploaded" servlet in JSON format
    in = urlEntity.getContent();
    str = "";
    while (true) {
        int ch = in.read();
        if (ch == -1)
            break;
        str += (char) ch;
    }
    JSONObject resultJson = new JSONObject(str);
    String blobKey = resultJson.getString("blobKey");
    String servingUrl = resultJson.getString("servingUrl");
    

    uploaded.java- servlet which returns Uploadurl and Blobkey of image

    BlobstoreService blobstoreService = BlobstoreServiceFactory
                .getBlobstoreService();
    
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
            try {
                List blobs = blobstoreService.getUploads(req).get("file");
                BlobKey blobKey = blobs.get(0);
    
                ImagesService imagesService = ImagesServiceFactory
                        .getImagesService();
                ServingUrlOptions servingOptions = ServingUrlOptions.Builder
                        .withBlobKey(blobKey);
    
                String servingUrl = imagesService.getServingUrl(servingOptions);
    
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.setContentType("application/json");
    
                JSONObject json = new JSONObject();
    
                json.put("servingUrl", servingUrl);
                json.put("blobKey", blobKey.getKeyString());
    
                PrintWriter out = resp.getWriter();
                out.print(json.toString());
                out.flush();
                out.close();
            } catch (JSONException e) {
    
                e.printStackTrace();
            }
    
        }
    

提交回复
热议问题