我正在使用blobstore API将图片上传到我的应用。这在本地工作正常,这意味着图像已成功上传,回调也可以。但是当我将multipart请求发送到createUploadUrl从服务器返回的uploadurl时,它总是给我404找不到错误。 是否有可能没有为我的应用启用blobstore?为什么我无法在Google云控制台中看到我的blobstore?我必须到这里https://appengine.google.com/blobstore/看看我的斑点。
应用程序端的代码:doPost处理回调,doGet返回uploadUrl(两者都在同一个servlet / _ah / admin / new_image中)。永远不会触发doPost(回调处理程序代码),也不会上传图像。
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeys = blobs.get("image");
ImagesService imagesService = ImagesServiceFactory.getImagesService();
String imageUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKeys.get(0)));
Long businessId = Long.parseLong(req.getParameter("business_id"));
String linkUrl = req.getParameter("link_url");
Long id = Long.parseLong(req.getParameter("advertisement_id"));
AdvertisementManager.addNewAdvertisement(id, imageUrl, linkUrl, businessId);
}
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
PrintWriter out = resp.getWriter();
resp.setContentType( "text/html" );
String uploadUrl = blobstoreService.createUploadUrl("");
Logger.getLogger("").log(Level.WARNING,uploadUrl);
out.print(uploadUrl);
}
上传图片的代码:首先发送获取请求以获取上传网址,然后发送带有图片文件的多部分请求
public static void upload(Long id, File file, String linkUrl, Long businessId){
HttpClient httpclientget = HttpClientBuilder.create().build();
HttpGet httpget = new HttpGet("http://app-url/_ah/admin/new_image");
String uploadUrl = "";
try {
HttpResponse responseGet = httpclientget.execute(httpget);
uploadUrl = EntityUtils.toString(responseGet.getEntity());
Logger.getLogger("").log(Level.WARNING,uploadUrl);
} catch (Exception e) {
e.printStackTrace();
}
HttpClient httpclient = HttpClientBuilder.create().build();
HttpPost httpPost = null;
String uri=null;
try {
URIBuilder builder = new URIBuilder(uploadUrl);
builder.setParameter("advertisement_id", id.toString());
builder.setParameter("link_url", linkUrl);
builder.setParameter("business_id", businessId.toString());
uri=builder.build().toString();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpPost = new HttpPost(uri);
System.out.println(uri);
if (file!=null){
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
reqEntity.addBinaryBody("image", file, ContentType.create("image/jpeg"), "ad.jpg");
httpPost.setEntity(reqEntity.build());
}
try {
HttpResponse response = httpclient.execute(httpPost);
System.out.println(response);
} catch (Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
答案 0 :(得分:1)
AFAIK / _ah / *空间是为内部GAE服务保留的,您不应该使用它。您获得404的原因可能是Google阻止您部署该servlet,请尝试更改URL。