gpt4 book ai didi

java - Google App Engine blobstore createUploadUrl 404 在服务器上找不到

转载 作者:行者123 更新时间:2023-11-30 08:04:29 26 4
gpt4 key购买 nike

我正在使用 blobstore API 将图像上传到我的应用程序。这在本地工作得很好,这意味着图像已成功上传并且回调也可以工作。但是,当我将多部分请求发送到服务器 createUploadUrl 返回的 uploadurl 时,它总是给我 404 未找到错误。我的应用程序是否可能未启用 blobstore?为什么我在 Google Cloud 控制台中看不到我的 Blobstore?我必须去这里https://appengine.google.com/blobstore/看看我的 Blob 。

应用程序端的代码: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);
}

上传图片的代码:先发送get请求获取上传url,然后发送带图片文件的multipart请求

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

最佳答案

据我所知,/_ah/* 空间是为内部 GAE 服务保留的,您不应该使用它。您收到 404 的原因可能是 Google 阻止您部署该 servlet,请尝试更改 URL。

关于java - Google App Engine blobstore createUploadUrl 404 在服务器上找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31348600/

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