0

在 GWT 中,如何在给定密钥的字符串版本的情况下显示来自 appengine 服务器端 blobstore 的图像?

我想我已经将图像存储为 appengine 上的 blob。有人可以告诉我它是否正确吗?

try 
                            {
                                FileService fileService = FileServiceFactory.getFileService();
                                AppEngineFile file = fileService.createNewBlobFile(content_type, fileName);
                                boolean lock = true;
                                FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
                                byte[] b1 = new byte[BUFFER_SIZE];
                                int readBytes1;
                                while ((readBytes1 = is.read(b1)) != -1) 
                                {
                                    writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
                                }

                                writeChannel.closeFinally();
                                item_image_blob_key = fileService.getBlobKey(file).getKeyString();
                            } 
                            catch (Exception e) 
                            {
                                System.out.println(e.getLocalizedMessage());
                                e.printStackTrace(response.getWriter());
                            }

我将密钥发回给客户,我正在尝试展示图像。我尝试使用:

ImagesService imagesService = ImagesServiceFactory
                .getImagesService();

        // Get the image serving URL
        String imageUrl = imagesService.getServingUrl(blobKey);

但它已被弃用,所以我尝试了:

 ImagesService imagesService = ImagesServiceFactory.getImagesService();
              ServingUrlOptions suo = ServingUrlOptions.Builder.withBlobKey(blobKey);
              String image_url = imagesService.getServingUrl(suo);

              item.setProperty("image_url", image_url);

现在我得到一个如下所示的 URL:

http://0.0.0.0:8888/_ah/img/5nXYgHwfiMmblDFldDXSew

并因此在客户端上创建图像:

String image_url = result.get_image_url();
                    System.out.println("image url is: " + image_url);

                    Image image = new Image();
                    image.setUrl(image_url);

                    RootPanel.get("dynamicDate").add(image);

UI 上仅显示默认图像图标

所以我创建了一个访问blobstore的servlet:

import java.io.IOException;

导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;

导入 com.google.appengine.api.blobstore.BlobKey;导入 com.google.appengine.api.blobstore.BlobstoreService;导入 com.google.appengine.api.blobstore.BlobstoreServiceFactory;

公共类 ImageServlet 扩展 HttpServlet { private static final long serialVersionUID = 1L;

@Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { String blob_key = req.getParameter("blob-key");

    if (blob_key != null) 
    {
      BlobKey blobKey = new BlobKey(blob_key);
      BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

      blobstoreService.serve(blobKey, res);
    } 
    else 
    {
      res.sendError(400, "One or more parameters are not set");
    }

} }

和一个客户端 http 请求:

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, "/itemmanager/image");

                    try 
                    {
                        Request request = requestBuilder.sendRequest(null, new RequestCallback() {

                        public void onError(Request request, Throwable exception) 
                        {
                           System.out.println(exception.getMessage());
                        }

                        public void onResponseReceived(Request request, Response response) 
                        {
                            System.out.println("so far so good");
                            System.out.println(response.getHeadersAsString());

                          if (200 == response.getStatusCode()) 
                          {

                          } 
                          else 
                          {
                            // Handle the error.  Can get the status text from response.getStatusText()
                          }
                        }       
                          });
                        } catch (RequestException e) {
                          // Couldn't connect to server        
                        }

我似乎正在获取文本我如何获取输入流或我可以获取图像的东西?

4

1 回答 1

0

经过两天的搜索谷歌和堆栈溢出并尝试后,我终于明白了!

在服务器上,我得到了上传网址:

 ImagesService imagesService = ImagesServiceFactory.getImagesService();
              ServingUrlOptions suo = ServingUrlOptions.Builder.withBlobKey(blobKey);
              String image_url = imagesService.getServingUrl(suo);

              item.setProperty("image_url", image_url);

appengine API 生成的 url 在本地模式下不起作用 [我认为这与 sop 跨平台问题有关]

http://0.0.0.0:8888/_ah/img/mR9SOTSEizec4gZYsRnuEw

但它提供了一个线索:即字符串的 /_ah/img/ 部分

所以我决定尝试一下,并将这个 URL 提供给图像“/_ah/img/mR9SOTSEizec4gZYsRnuEw” 这是客户端代码。

String imageKey = result.get_image_key();
                    System.out.println("category is: " + result.get_category() + "   image blob key is: " + imageKey);

                    String image_url = result.get_image_url();
                    System.out.println("image url is: " + image_url);

                    //This doesn't work at least locally
                    /*Image image = new Image();
                    image.setUrl(image_url);

                    RootPanel.get("dynamicDate").add(image);*/

                    Image image2 = new Image();
                    image2.setUrl("/_ah/img/" + imageKey);

                    RootPanel.get("dynamicDate").add(image2);
于 2013-06-28T07:52:32.243 回答