如何使用驱动器 api java 从谷歌驱动器下载文件?

How to download a file from google drive using drive api java?(如何使用驱动器 api java 从谷歌驱动器下载文件?)
本文介绍了如何使用驱动器 api java 从谷歌驱动器下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我尝试了谷歌驱动的一些功能,但我无法从驱动器下载文件.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)