我的 GAE 的 blobstore 中有一个图像。我需要检索它并进行一些转换,最后将其显示在我的 jsp 页面中。
目前我使用的是,
BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
blobstoreService.serve(blobKey, res);
这显示了图片,但我想将其作为“图像”类型检索并使用下面的代码调整其大小
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize = ImagesServiceFactory.makeResize(200, 300);
Image newImage = imagesService.applyTransform(resize, oldImage);
byte[] newImageData = newImage.getImageData();
如何在 jsp 页面中显示我的“newImage”?如果我能看到示例代码,那将非常有帮助。我还想知道是否有任何方法可以获得我目前在 blobviewer 中的图像的 blob-key。
最佳答案
您不应该处理 JSP 页面请求中的图像。 您必须采取两个步骤:
1. 渲染包含图像标记的 JSP 页面,例如
<img src="mydomain.com/getImage?blob-key=123435"/>
2. 有一个单独的servlt映射到mydomain.com/getImage,输出具有给定id的图像。
因此,您上面提供的所有代码都将放入传递图像的 servlet 中,而不是放入传递代码的 JSP 中。然后可以使用 HTTPResponses OutputStream 传递图像。并且不要忘记为响应设置正确的内容类型和长度。
关于在 Google App Engine 中显示图像的 Java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7554745/