gpt4 book ai didi

java - 为什么上传图片到Blobstore后回调失败?

转载 作者:太空狗 更新时间:2023-10-29 13:22:12 25 4
gpt4 key购买 nike

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

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

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

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

这是从 post 方法返回。当我部署到应用引擎并从那里开始工作时,出现了同样的错误。

我的代码如下。

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

}

安卓客户端

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;

网络.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>

提前致谢。

最佳答案

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

关于java - 为什么上传图片到Blobstore后回调失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27233890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com