Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 谷歌应用引擎使用Blobkey_Java_Eclipse_Google App Engine_Cloud_Blobstore - Fatal编程技术网

Java 谷歌应用引擎使用Blobkey

Java 谷歌应用引擎使用Blobkey,java,eclipse,google-app-engine,cloud,blobstore,Java,Eclipse,Google App Engine,Cloud,Blobstore,大家好,我正在尝试制作一个servlet,允许管理员上传图片,任何谷歌用户都可以查看这些图片,到目前为止,我正在开发一个可以在 当我上传一张图片时,它会直接使用一个很长的blobKey来服务它?并将其自身的副本存储在本地_db.bin中 我不知道是否有任何方法可以缩短blobkeys以供使用?例如,我想有一个画廊,其中显示所有已上传的图像,由用户,但到目前为止,我可以从数据库中获得图像的唯一方式是调用这样的东西 res.sendRedirect/service?blob key=+blobKey

大家好,我正在尝试制作一个servlet,允许管理员上传图片,任何谷歌用户都可以查看这些图片,到目前为止,我正在开发一个可以在

当我上传一张图片时,它会直接使用一个很长的blobKey来服务它?并将其自身的副本存储在本地_db.bin中

我不知道是否有任何方法可以缩短blobkeys以供使用?例如,我想有一个画廊,其中显示所有已上传的图像,由用户,但到目前为止,我可以从数据库中获得图像的唯一方式是调用这样的东西

res.sendRedirect/service?blob key=+blobKey.getKeyString

但这只适用于一个图像,我需要对每个新blobKey进行硬编码,以便在单独的页面上显示它,这也意味着当用户上载新图像时,我必须编辑代码并为新图像添加新链接

基本上,我想知道的是,是否有任何方法可以轻松定义本地_db.bin中存储的每个blob

任何帮助都将不胜感激,请不要犹豫,询问更多细节


谢谢

我认为你处理问题的方式有点尴尬

它给你这个blob密钥并不是Blobstore的问题。您可以做的是:

创建上载servlet以捕获文件上载 获取字节并使用AppEngine文件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发送到客户端:

比如一个带有查询参数的@GET资源,比如?filename=随便什么 然后,您将从数据存储中获取与此文件名关联的blobkey 这只是一个简化的示例,您必须确保将Filename和Blobkey保存在正确的容器和用户中(如果需要)


您可以直接使用Blobstore API和Image API,但如果需要进一步控制,则必须设计自己的API。反正也没那么难,ApacheJersey和JBossRestEasy与GAE完美配合。

您可以使用GAE的图像服务api来服务GAE的图像。在@user2216137这篇帖子中看到我的解决方案,我的答案有用吗?