問題描述
我嘗試了谷歌驅動的一些功能,但我無法從驅動器下載文件.
I have tried some functions of Google drive, but I couldn't able to download the file from Drive.
這是我使用的代碼
private static InputStream downloadFile(Drive service, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp =
service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
當我運行腳本時,它顯示 file.getDownloadUrl()
為 NULL.
When I ran the script it shows file.getDownloadUrl()
is NULL.
我錯過了什么?
在我調用下載函數之前添加以下行后它正在執行
Now it's executing after adding the following line before I call the download function
File file1 = service.files().get(fileid).execute();
downloadFile(service,file1);
現在的問題是如何借助我從腳本中獲得的響應"來下載文件....
Now the problem is how to download the file with the help of 'response' which I got from the script....
推薦答案
Google Docs 原生格式的文檔沒有 downloadUrl
字段,您可以使用 exportLinks
集合:
Documents in Google Docs native formats won't have a downloadUrl
field, instead you can export them using the exportLinks
collection:
https://developers.google.com/drive/manage-downloads#downloading_google_documents
這篇關于如何使用驅動器 api java 從谷歌驅動器下載文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!