[英]Upload file to Google cloud storage from Google App Engine (Java)
[英]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.