2012-06-03 596 views
1

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

这个文件下载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(); 
     } 

回答

0

你试试下面的语句顺序。

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

int b=0; 
... 
1

而不是自己尝试流式传输文件,您应该使用BlobstoreService.serve方法。这照顾或流媒体,可用于任何大小的文件。

喜欢的东西

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

感谢愿意尝试这个修复,但日食是抛出一个语法错误的createGsBlobKey方法 - “的方法createGsBlobKey(字符串)是未定义的类型BlobstoreService”。我正在使用AppEngine SDK 1.6.3。 – user1434130

+0

需要1.6.5或更好。 –

+0

升级到1.6.6。问题仍然存在,在本地环境中工作正常,但是当我部署到服务器时问题仍然存在。适用于文本文件,但不适用于任何其他格式。 我是否必须启用计费才能使其始终如一地工作?到目前为止,我在免费配额内运作。 – user1434130

相关问题