java - Google App Engine for Java 和 Google 云存储

标签 java google-app-engine google-cloud-storage

我有一个基于 Google App Engine (Java) 的应用程序,它将文件数据存储在 Google Cloud 存储中。

此文件下载 servlet 在我的本地 eclipse 环境中工作正常,并且当部署到 appspot 域时,这适用于简单的文本文件,但适用于任何文档(在新选项卡中的浏览器中显示),但如果我尝试使用任何其他二进制文件(doc、jpeg、gif 等)似乎什么也不做,服务器端也不会抛出任何错误。我直接检查了 Google Cloud 存储中的文件夹,文件存储正确并且能够直接访问,但无法通过应用引擎执行此操作。

如果我遗漏了什么,可以告诉我吗?

下面的代码片段,

         try {
             FileService newfileService = FileServiceFactory.getFileService();
             AppEngineFile file = new AppEngineFile(cloudpath) ;
             FileReadChannel channel = newfileService.openReadChannel(file, false);
             BufferedInputStream bis = new BufferedInputStream(Channels.newInputStream(channel));
             BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());
             resp.setHeader("Content-Disposition", "inline;filename=\"" + file.getNamePart() + "");
             int b = 0;
             while((b = bis.read()) != -1) {
                 bos.write(b);
             }
             bos.flush();
         } catch (Exception e) {
             e.printStackTrace();
         }

最佳答案

您应该使用 BlobstoreService.serve 方法,而不是尝试自己流式传输文件。这需要小心或流式传输,并且可以用于任何大小的文件。

类似于

BlobstoreService blobService = BlobstoreServiceFactory.getBlobstoreService();
blobService.serve(blobService.createGsBlobKey(cloudpath), resp);

关于java - Google App Engine for Java 和 Google 云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10874659/

相关文章:

java - Liferay 6.2自定义连接同名列重叠

google-app-engine - Google App Engine 上的 PIL 可以使用哪些系统字体?

google-app-engine - 如何在 Google App Engine 数据存储中存储多维数组

Firebase 函数 1.0.0 迁移 : Trouble with custom initializeApp() with Google Service Account credential

python - 云函数将 CSV 发送到云存储

java - 如何在 Selenium 2.0 中的动态菜单项的鼠标悬停上执行鼠标单击

java - 如何使用 java.util 的链接创建文档(原始文档)

python - pip 将本地包安装到目标目录

java - 为什么我无法通过 App Engine 将大于 100KByte 的文件上传到 Google Cloud Storage?

java - 错误包 org.apache.pig.FilterFunc 不存在