从android上传Blobstore中的图片?

时间:2016-10-22 11:23:40

标签: java android google-app-engine servlets blobstore

您好我将我的图像作为Base64编码的字符串发送到servlet。从他们想要将我的图像存储在blobstore中并在需要时进行检索。 我接下来的步骤是: 1.将图像数据作为Base64编码的字符串从Asynctask发送到我的Servlet。 2.我创建了两个类名BlobUrlGet.java,第二个名为BlobUpload.java,如下所示。

public class BlobUrlGet extends HttpServlet{

Logger logger=Logger.getLogger("ImageUpload.class");
BlobstoreService blServ = BlobstoreServiceFactory.getBlobstoreService();

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

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub

    String blobUploadUrl = blServ.createUploadUrl("/blobUpload");
    logger.log(Level.SEVERE,"blobUploadUrl:::::::::::::"+blobUploadUrl);
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.setContentType("text/plain");

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

}
}

这是BlobUpload.java类

    public class BlobUpload extends HttpServlet {

    BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        try {
            List<BlobKey> blobs = blobstoreService.getUploads(req).get("photo");
            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 (Exception e) {

            e.printStackTrace();
        }

    }

}

这是我的Servlet:

    public class ImageUploadServlet extends HttpServlet {

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        doPost(req, resp);
    }




    @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {


                String mapImageStr=req.getParameter("mapImage");
    HttpClient httpClient = new DefaultHttpClient();    
                    //This will invoke "ImgUpload servlet           
                    HttpGet httpGet = new HttpGet("http://MYLINK/blobUrlGet"); 
                    HttpResponse response;
                    try {
                        response = httpClient.execute(httpGet);         
                        HttpEntity urlEntity = response.getEntity();
                        InputStream in = urlEntity.getContent();
                        String str = ""; 
                        StringWriter writer = new StringWriter();
                        String encoding = "UTF-8";
                        IOUtils.copy(in, writer, encoding);
                        str = writer.toString();
                            HttpPost httppost = new HttpPost(str);
                            byte[] imgByte=Base64.decode(mapImageStr.toCharArray());
    //                      Image image=ImagesServiceFactory.makeImage(imgByte); 
                            MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
                            reqEntity.addBinaryBody("photo", imgByte, ContentType.create("image/jpeg"), "sampleImage.jpg");
                            httppost.setEntity(reqEntity.build());
                            response = httpClient.execute(httppost); //Here "uploaded" servlet is automatically       invoked
                            str = EntityUtils.toString(response.getEntity());
                            JSONObject resultJson = new JSONObject(str);
                            String blobKey = resultJson.getString("blobKey");
                            String servingUrl = resultJson.getString("servingUrl");
                            logger.log(Level.SEVERE,"blobKey::::::"+blobKey);
                            logger.log(Level.SEVERE,"servingUrl::::::"+servingUrl);


                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        logger.log(Level.SEVERE,"servingUrl::::::"+e);
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        logger.log(Level.SEVERE,"servingUrl::::::"+e);
                        e.printStackTrace();
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        logger.log(Level.SEVERE,"servingUrl::::::"+e);
                        e.printStackTrace();
                    }

    }

我的web.xml代码

<servlet>
        <servlet-name>blobUrlGet</servlet-name>
        <servlet-class>MYPACKAGE.BlobUrlGet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>blobUrlGet</servlet-name>
        <url-pattern>blobUrlGet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>blobUpload</servlet-name>
        <servlet-class>MYPACKAGE.BlobUpload</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>blobUpload</servlet-name>
        <url-pattern>blobUpload</url-pattern>
    </servlet-mapping>

我被困了两天以来一直在努力。我没有得到它。我们还有其他办法吗?我正在使用android studio进行Android客户端开发。

0 个答案:

没有答案