你好,我正在尝试制作一个 servlet,允许管理员上传图像,并允许任何谷歌用户查看这些图像,到目前为止,我正在研究可用的程序https://developers.google.com/appengine/docs/java/blobstore/overview
当我上传图像时,它会使用很长的 blobKey 立即提供服务?并将其自身的副本存储在 local_db.bin 中
我不知道是否有任何方法可以缩短使用时的 blobkeys?例如,我想要一个画廊,显示用户上传的所有图像,但是到目前为止,我从数据库获取图像的唯一方法是调用类似的方法
res.sendRedirect("/serve?blob-key=" + blobKey.getKeyString())
但这仅适用于一张图像,我需要对每个新的 blobKey 进行硬编码,以便将其显示在单独的页面上,这也意味着当用户上传新图像时,我将必须编辑代码并为新图像添加新链接图像?
基本上我想知道的是是否有办法轻松定义存储在 local_db.bin 中的每个 blob。
如有任何帮助,我们将不胜感激,请随时询问更多详细信息。
Thanks
我认为你正在以一种有点尴尬的方式处理你的问题。
它为您提供此 blob 密钥不是 Blobstore 问题。你能做的是:
- 创建上传 servlet 来捕获文件上传
- 获取字节并使用 AppEngine File API 存储它
让我向您展示(我的项目中的工作代码块):
@POST
@Consumes("multipart/form-data")
@Path("/databases/{dbName}/collections/{collName}/binary")
@Override
public Response createBinaryDocument(@PathParam("dbName") String dbName,
@PathParam("collName") String collName,
@Context HttpServletRequest request, @Context HttpHeaders headers,
@Context UriInfo uriInfo, @Context SecurityContext securityContext) {
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator fileIterator = upload.getItemIterator(request);
while (fileIterator.hasNext()) {
FileItemStream item = fileIterator.next();
if ("file".equals(item.getFieldName())){
byte[] content = IOUtils.toByteArray(item.openStream());
logger.log(Level.INFO, "Binary file size: " + content.length);
logger.log(Level.INFO, "Mime-type: " + item.getContentType());
String mimeType = item.getContentType();
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mimeType);
String path = file.getFullPath();
file = new AppEngineFile(path);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
writeChannel.write(ByteBuffer.wrap(content)); // This time we write to the channel directly
writeChannel.closeFinally();
BlobKey blobKey = fileService.getBlobKey(file);
} else if ("name".equals(item.getFieldName())){
String name=IOUtils.toString(item.openStream());
// TODO Add implementation
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
正如您所看到的,Blobstore 只是提供“图像”的一部分,您必须为自己创建一个 API 或其他东西,将这些图像或任何二进制数据获取到 Blobstore,包括将其文件名保存到数据存储区。
您必须做的另一件事是使用 API 或接口将其从 Blobstore 发送到客户端:
- Like a
@GET
具有查询参数的资源,例如?filename=whatever
- 然后,您将从数据存储中获取与该文件名关联的 blobkey
这只是一个简化的示例,您必须确保保存文件名和 Blobkey,即如果需要,保存在正确的容器和用户中。
您可以直接使用 Blobstore API 和 Image API,但如果您需要进一步控制,则必须设计自己的 API。无论如何,这并不难,Apache Jersey 和 JBoss Resteasy 与 GAE 完美配合。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)