将图像上传到Blobstore后,为什么回调失败?

时间:2014-12-01 17:31:02

标签: java android servlets http-post blobstore

我正在尝试将照片上传到blobstore。照片会成功上传到blobstore,但我无法获得blobkey和servingUrl。我试过调试,发现没有对BlobUpload.java进行回调。它给出了一个错误'连接到http:// localhost:8888拒绝'我很困惑因为BlobUrlGet.java没有问题调用并且照片上传了。我尝试了服务器和servlet的每种类型的URL,但没有一个工作。

编辑:我意识到如果我在调试模式(str变量)中手动将'localhost:8888'更改为'10 .0.2.2:8888','连接被拒绝'问题就会消失但是这次它显示以下错误。

http:// localhost:8888 / _ah / upload / ahFteWZpcnN0YXBwZGVtbzEyM3IiCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGICAgICAgN4KDAError 405 HTTP方法此URL不支持POST

错误405 HTTP方法此URL不支持POST

这是post方法的返回。当我部署到appengine并从那里工作时,这是同样的错误。

我的代码如下。

BlobUrlGet.java

public class BlobUrlGet extends HttpServlet{   
    BlobstoreService blServ = BlobstoreServiceFactory.getBlobstoreService();    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {  
        String blobUploadUrl = blServ.createUploadUrl("/blob");   
        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();

    public void doPost(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客户端

private class GetBlobUrlTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0){  

        HttpClient httpClient = new DefaultHttpClient();    
        //This will invoke "ImgUpload servlet           
        HttpGet httpGet = new HttpGet("http://10.0.2.2:8888/bloburl"); 
        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();
//              str = URLDecoder.decode(str, "UTF-8");
                HttpPost httppost = new HttpPost(str);
                File f = new File(picturePath);
                FileBody fileBody = new FileBody(f);
                MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
                reqEntity.addPart("file", fileBody);
                reqEntity.addBinaryBody("Photo", f, ContentType.create("image/jpeg"), "foto2.jpg");
                httppost.setEntity(reqEntity.build());
                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 = "";
                IOUtils.copy(in, writer, encoding);
                str = writer.toString();
                JSONObject resultJson = new JSONObject(str);
                blobKey = resultJson.getString("blobKey");
                servingUrl = resultJson.getString("servingUrl");    



        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

的web.xml

  <servlet>
    <servlet-name>BlobstoreURL</servlet-name>
    <servlet-class>com.google.samplesolutions.mobileassistant.BlobUrlGet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BlobstoreURL</servlet-name>
    <url-pattern>/bloburl</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>BlobstoreUpload</servlet-name>
    <servlet-class>com.google.samplesolutions.mobileassistant.BlobUpload</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BlobstoreUpload</servlet-name>
    <url-pattern>/blob</url-pattern>
  </servlet-mapping>

提前致谢。

1 个答案:

答案 0 :(得分:2)

doPost应该被覆盖。现在它完美无缺。

public class BlobUpload extends HttpServlet {
    BlobstoreService blobstoreService = BlobstoreServiceFactory
            .getBlobstoreService();
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        try {
            List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");
            BlobKey blobKey = blobs.get(0);