【问题标题】:download the files from Gdrive to local system by using java使用 java 将文件从 Gdrive 下载到本地系统
【发布时间】:2013-07-18 10:15:33
【问题描述】:

Drive 快速入门:在 Java 中运行 Drive App 示例适用于上传文件。我想使用 java 将文件从 Gdrive 下载到本地系统。 对于下载,它们被赋予了一个方法

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

上述方法,我该如何输入?我从哪里输入?任何人都可以提供完整的下载代码,例如快速入门上传类。

我们将不胜感激。

【问题讨论】:

    标签: java file download google-drive-api


    【解决方案1】:

    你可以使用google drive api并发送Http获取请求,你可以看到这个教程

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

    【讨论】:

    • 感谢您的回复。我的问题是我需要为方法 downloadFile(Drive service, File file) 中的参数 Drive 和 File 提供什么输入。
    • 您在云端硬盘使用哪个 api?你可以为这个 api 阅读文档,它会帮助你
    【解决方案2】:

    感谢 Hanan,它工作正常。通过使用 retrieveAllFiles(),我可以列出所有文档。并且我已经使用下面的代码将检索到的文档存储在我的本地。这是一种正确的下载方式。

    for(File f:result){         
            i++;
            System.out.println("File Name==>"+f.getTitle());
            System.out.println("File Id==>"+f.getId());
            System.out.println("File ext==>"+f.getFileExtension());
            System.out.println("File size==>"+f.getFileSize());
    InputStream in = downloadFile(service,f);
            byte b[] = new byte[in.available()];
            in.read(b);
            java.io.File ff = new java.io.File("/home/test/Desktop/gdocs/"+f.getTitle());
            FileOutputStream fout = new FileOutputStream(ff);
            fout.write(b);
            fout.close();
    } 
    

    它将所有文档存储在本地。文本 (.txt) 文件在我的本地正确打开,但图像文件或 pdf 文件未正确打开。它给出了一些错误消息,如文件损坏。图像或 pdf 文档中没有内容我如何获取内容并存储它。任何建议

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多