【发布时间】:2015-09-18 15:20:59
【问题描述】:
我一直在参考这个帖子Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE。下面的最后两个代码块可能是最相关的,因为它们是在应用引擎和我的 android 应用之间发送和接收 blob 密钥的代码。
blob 正在上传。 当我让我的 servlet 返回 blob 密钥时,我会继续在我的日志中得到这个:
405 HTTP method POST is not supported by this URL
这是我在应用引擎上的 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 应该返回 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();
为什么我的 String blobKeyString 在返回到 android 应用程序时,在正文中继续具有 html 文档的值:
405 HTTP method POST is not supported by this URL
【问题讨论】:
-
所以如果你去掉进攻线的印刷品,你不会得到“方法POST”的错误吗?对我来说,查看错误,我会说接收 blob 的 URL 只需要一个“doPost”方法
-
@Patrice。感谢您指出了这一点。我将 doGet 更改为 doPost,现在效果很好。不敢相信我没有看到。谢谢!如果您将其写在答案中,我会将其标记为答案。
-
非常欢迎!总是乐于帮助平台上的用户 :) 将其发布为答案 :)
标签: java android html google-app-engine google-cloud-storage