几天来我一直坚持这个特殊的问题而没有任何成功,所以我认为是时候伸出援手了 .

我在jsp中有一个表单,它是使用Struts2运行的Google App Engine Web应用程序的一部分 . 我的用例是这个表单应该用于将“Tile”上传到我的后端存储 . 表单包含文本字段,复选框和文件字段,用于上传代表Tile的图像(对于上下文,Tile用于游戏) . 文本/布尔字段将被对象化为“平铺”实体并存储在Google数据存储区中,该数据存储区目前正在运行并按预期工作,图像文件将上传到Google Cloud 端存储并保存在存储桶中 . 但是,我的问题在于图片上传 .

我已经尝试了几种方法但没有成功,但我最接近的是使用Storage.Objects资源将流上传到商店 . 我目前使用Spring将表单内容合并到一个“Tile”bean中,该bean包含所有字段,包括图像文件(该文件当前存储为com.google.appengine.api.datastore.Blob,属于a不同的API,实际上可能是问题的原因,但我似乎无法找到存储替代品) . 提交后,此tile对象通过我的操作传递给DAO,Blob被提取,并且Tile(没有图像blob)被持久化到数据存储区 . 这部分工作正常 . 然后,我从图像Blob中提取字节,并围绕它们构造输入流,并将此流输出到存储 .

public void storeTileImageToStorage(Blob imageData, String fileName) throws FileNotFoundException, IOException, GeneralSecurityException {

        ByteArrayInputStream bis = new ByteArrayInputStream(imageData.getBytes());

        uploadStreamToStorage(fileName, "image/png", bis, bucketName);
    }


public static void uploadStreamToStorage(
            String name, String contentType, InputStream stream, String bucketName)
            throws IOException, GeneralSecurityException
    {
        InputStreamContent contentStream = new InputStreamContent(contentType, stream);

        StorageObject objectMetadata = new StorageObject()
                // Set the destination object name
                .setName(name).setContentType(contentType);

        // Do the insert
        Storage client = getStorageService();
        Storage.Objects.Insert insertRequest = client.objects().insert(
                bucketName, objectMetadata, contentStream);

        insertRequest.execute();
    }

这在某些方面有效,因为在存储桶中创建了具有正确名称(和文件大小)的新对象 . 但是,当我尝试使用网络客户端打开图像时,我会看到一个小白框,而不是实际图像(见下文) . 以编程方式为图像提供服务的任何尝试也会失败

Image

编辑:根据要求,我用来检索图像的代码 . 虽然我不相信这是相关的,因为当我点击 Cloud 存储网络客户端本身的文件时出现“白盒子”,而不是当我尝试以编程方式服务图像时 .

编辑2:再次查看此代码,我意识到这将无法正常工作,因为.get()和强制转换为Blob将不会像我打算那样工作 . 但是,无论服务代码目前不是我的问题,存储中文件本身的格式是当前的问题 .

public Blob retrieveTileImageFromStorage(String tileName) throws IOException, GeneralSecurityException {
        StorageObject object = getObjectFromStorageBucket(bucketName, tileName);
        Blob image = (Blob)object.get(tileName);

        return image;
    }

public static StorageObject getObjectFromStorageBucket(String bucketName, String objectName)
            throws IOException, GeneralSecurityException {
        Storage client = getStorageService();
        Storage.Objects.Get get = client.objects().get(bucketName, objectName);

        StorageObject object = get.execute();

        return object;
    }

我假设会有一个解决方案,我会犯一个愚蠢的错误,因为文件大小正确,暗示上传确实有效 . 我只是将文件视为图像时遇到问题 . 任何人都可以对这种情况有所了解吗?提前致谢 .