繁体   English   中英

适用于Java和Google云存储的Google App Engine

[英]Google App Engine for Java and Google Cloud Storage

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

此文件下载servlet在我的本地Eclipse环境中运行良好,并且在部署到appspot域时,它适用于简单的文本文件,但适用于任何文档(显示在浏览器中的新标签中),但是如果我尝试使用任何其他二进制文件(doc ,jpeg,gif等)似乎什么也不做,在服务器端也不会引发任何错误。 我直接在Google云端存储中的文件夹中进行了检查,文件已正确存储并且可以直接访问,但无法通过应用程序引擎进行访问。

你能不能让我知道我是否缺少什么?

下面的代码段

         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);

您将尝试以下语句顺序。

...
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition", "inline;filename=\"" + file.getNamePart() + "");
BufferedOutputStream bos = new BufferedOutputStream(resp.getOutputStream());

int b=0;
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM