在 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
}
我似乎正在获取文本我如何获取输入流或我可以获取图像的东西?