店面形象来自Android客户端Blob存储和检索blobkey和上传的URL存储在数据存储。 - GAE [英] Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE

查看:120
本文介绍了店面形象来自Android客户端Blob存储和检索blobkey和上传的URL存储在数据存储。 - GAE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想图像上传到Blob存储,然后检索上传网址和图像的Blobkey,这样我就可以存储Blobkey中的数据存储。

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.

我已经试过这code,但我的图像不会上传:

I've tried this code, but my image isn't uploading:

Servlet的(返回上传的URL)

Servlet (Return upload url)

BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();
public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        UploadOptions uploadOptions = UploadOptions.Builder
                .withGoogleStorageBucketName("photobucket11")
                .maxUploadSizeBytes(1048576);
        String blobUploadUrl = blobstoreService.createUploadUrl("/upload",
                uploadOptions);

        // String blobUploadUrl = blobstoreService.createUploadUrl("/uploaded");

        resp.setStatus(HttpServletResponse.SC_OK);
        resp.setContentType("text/plain");

        PrintWriter out = resp.getWriter();
        out.print(blobUploadUrl);

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        doGet(req, resp);
    }

code:Android客户端

Code : Android client

Bitmap bmp = BitmapFactory.decodeFile(imagePath);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                bmp.compress(CompressFormat.JPEG, 75, out);
                byte[] imgByte = out.toByteArray();
                String encodedImage = Base64.encodeToString(imgByte,
                        Base64.DEFAULT);

                HttpClient httpClient = new DefaultHttpClient();                    
                HttpGet httpGet = new HttpGet(
                        "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;
                }

这将返回上传网址的形式/ _啊/上传/ akjdhjahdjaudshgaajsdhjsdh ,我可以用它来存储图像。

This will return upload url in form of /_ah/upload/akjdhjahdjaudshgaajsdhjsdh which I can use to store the image.

这code使用的URL来存储图像:

This code uses the url to store the image:

httpClient = new DefaultHttpClient();
                HttpPost postRequest = new HttpPost(str);
                ByteArrayBody bab = new ByteArrayBody(imgByte, "forest.jpg");

                MultipartEntity reqEntity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);

                reqEntity.addPart("uploaded", bab);
                reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
                postRequest.setEntity(reqEntity);
                response = httpClient.execute(postRequest);

                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(
                                response.getEntity().getContent(), "UTF-8"));
                String sResponse;
                StringBuilder s = new StringBuilder();

                while ((sResponse = reader.readLine()) != null) {
                    s = s.append(sResponse);
                }

在这里,如果我检查字符串取值的值,它显示了。这意味着它返回一个空的响应。我不知道问题出在这个code什么。请指导我解决这个问题。

Here, if I check value of the String s, it shows null. That means it is returning a null response. I don't know what the problem is with this code. Please guide me to solve this problem.

推荐答案

多次尝试之后,我解决了这个问题。要保存图像Blob存储区,第一款Android需要做出请求的servlet将产生上传网址:

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客户端:将请求生成URL和servlet的获取网址

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来生成URL并发送响应客户端

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

在Android客户端,下面写code上传图片,从上面的servlet返回响应。

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返回的uploadURL和形象Blobkey

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

    }

这篇关于店面形象来自Android客户端Blob存储和检索blobkey和上传的URL存储在数据存储。 - GAE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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