2012-06-03 587 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

相關問題