如何使用驱动器api java从谷歌驱动器下载文件? [英] How to download a file from google drive using drive api java?

查看:108
本文介绍了如何使用驱动器api java从谷歌驱动器下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了Google驱动器的某些功能,但无法从Drive下载文件。



以下是我使用的代码

  private static InputStream downloadFile(Drive service,File file){
if(file.getDownloadUrl()!= null&& amp ; file.getDownloadUrl()。length()> 0){
try {
HttpResponse resp =
service.getRequestFactory()。buildGetRequest(new GenericUrl(file.getDownloadUrl()))
.execute();
return resp.getContent();
} catch(IOException e){
//发生错误。
e.printStackTrace();
返回null;
}
}其他{
//文件没有存储在驱动器上的任何内容。
返回null;


当我运行脚本时,它显示 file.getDownloadUrl()为NULL。



我在这里错过了什么?



现在,在我调用下载函数之前添加以下行,它正在执行。

  File file1 = service.files()。得到(FILEID).execute(); 
downloadFile(service,file1);

现在问题是如何在'response'的帮助下下载文件,脚本....

解决方案

Google Docs原生格式的文档不会有 downloadUrl 字段,您可以使用 exportLinks 集合导出它们:

https://developers.google.com/drive/manage-downloads#downloading_google_documents


I have tried some functions of Google drive, but I couldn't able to download the file from Drive.

Here is the code which I used

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

When I ran the script it shows file.getDownloadUrl() is NULL.

What did I miss here?

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....

解决方案

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从谷歌驱动器下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆