使用 HTTP 方法从 blobstore 检索 blob 密钥

Retrieving blob key from blobstore with HTTP method

我一直在参考这个 post Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE 。下面的最后两个代码块可能是最相关的,因为它们是在应用引擎和我的 android 应用程序之间发送和接收 blob 密钥的代码。

Blob 上传正常。 当我有我的 servlet return blob 键时,我继续在我的日志中得到这个:

405 HTTP method POST is not supported by this URL

这是我在 App Engine 上的 BlobUrlGet.java,它为 blob 的上传提供了 URL:

public class BlobUrlGet extends HttpServlet {


BlobstoreService blServ =    BlobstoreServiceFactory.getBlobstoreService();

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


    UploadOptions uploadOptions = UploadOptions.Builder.withGoogleStorageBucketName("abc123-974.appspot.com");
    String blobUploadUrl = blServ.createUploadUrl("/Uploaded", uploadOptions);




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

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

}

然后我在我的 android 应用程序中使用此代码上传文件,然后侦听 blob 键:

private class GetBlobUrlTask extends AsyncTask<Void, Void, Void> {
    HttpResponse response = null;

    protected Void doInBackground(Void... arg0){  
HttpClient httpClient = new DefaultHttpClient();  

HttpGet httpGet = new HttpGet("http://abc123-974.appspot.com/bloburlget"); 


        response = httpClient.execute(httpGet);


HttpEntity urlEntity = response.getEntity();
InputStream in = null;
        in = urlEntity.getContent();


}
String str = ""; 
StringWriter writer = new StringWriter();
String encoding = "UTF-8";


    IOUtils.copy(in, writer, encoding);

str = writer.toString();



@SuppressWarnings("deprecation")
HttpPost httppost = new HttpPost(str);

File f2 = new File(Environment.getExternalStorageDirectory()
        + "/ContactDetail.json");
FileBody fileBody = new FileBody(f2);


MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file", fileBody);
httppost.setEntity(reqEntity);

    response = httpClient.execute(httppost);




String str2 = "";

str2 = EntityUtils.toString(response.getEntity());

//Getting that Post is not supported method when I print the following string:
blobKeyString = str2;

最后我的 Uploaded.java 应该是 return blob 键,但它不是:

public class Uploaded extends HttpServlet {


private static final long serialVersionUID = 1L;
BlobstoreService blobstoreService = BlobstoreServiceFactory
        .getBlobstoreService();

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

            List<BlobKey> blobs = blobstoreService.getUploads(req).get("file");

            BlobKey blobKey = blobs.get(0);

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

            JSONObject json = new JSONObject();

           String blobKeyString = blobKey.getKeyString();

            PrintWriter out = resp.getWriter();
            out.print(blobKeyString);
            out.flush();
            out.close();

为什么我的字符串 blobKeyString 在返回 android 应用程序时继续具有 html 文档的值,正文中包含此内容:

405 HTTP method POST is not supported by this URL

查看错误,这意味着您需要一种方法来处理对应用的 POST 请求。

我会更改 URL 上接收请求的 servlet,使其具有 "doPost" 而不是 "doGet"。