Google Drive API 不下载文件 (Java v3)

2024-01-31

我正在使用这里的代码:https://developers.google.com/drive/api/v3/manage-downloads#downloading_a_file https://developers.google.com/drive/api/v3/manage-downloads#downloading_a_file

我使用的代码片段如下并放置在 main 方法中:

    String fileId = "some file ID";
    OutputStream outputStream = new ByteArrayOutputStream();
    driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

我没有发现代码实际下载文件的迹象,也不知道文件实际下载后在哪里。 我不确定我是否使用正确的范围来获得下载文件的权限。只要知道 fileID,我就可以上传、列出和删除文件,但下载似乎不起作用。

    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE);

或者,我尝试创建一种方法来制定下载协议,如下所示:

    private static void downloadFile(Drive service, File file (or String fileID)){
    }

但我不确定该怎么做。我尝试过在线寻找示例,但大多数来自 v1 或 v2 api,似乎对我不起作用。

另外,我在某处读到无法下载文件夹。相反,我必须逐一下载文件夹中的每一项。 那么我是否必须创建一个 fileID 的 Arraylist/list/array 并在初始化变量来表示 fileID 后对其进行迭代?

编辑:已经取得了一些进展,但我仍然有一些问题正在努力解决。

    List<File> files = result.getFiles();
    File newFile;
    if (files == null || files.isEmpty()) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getName(), file.getId());
            String fileId = file.getId();
            //System.out.println(fileId);
            String fileName = file.getName();
            //System.out.println(fileName);
            OutputStream outputstream = new FileOutputStream();
            service.files().get(fileId)
            .executeMediaAndDownloadTo(outputstream);
            outputstream.flush();
            outputstream.close();
    }

我想要的是: 上面的代码在main方法中。我不知道这是否是正确的方法,但是当程序获取每个文件并执行 System.out.printf 时,我也希望它下载该文件(具有相同的 mimeType 和 pref 相同的名称)也)到 OutputStream 构造函数中设置的目标(C://User//some name//Downloads)。

我尝试过的: 根据我的测试,它仅按照我想要的方式下载第一个文件,但这只是因为我在 OutputStream 中指定了名称和扩展名。我已经初始化了变量“fileId”和“fileName”,以便当程序获取下一个文件的元数据时它们将根据信息进行更改,但我不知道如何在这段代码中更改或设置多个构造函数:

    OutputStream outputstream = new FileOutputStream();
            service.files().get(fileId)
            .executeMediaAndDownloadTo(outputstream);

下载所有文件。

我在 Google Drive 中的文件夹层次结构如下:

Logs

--bin(文件夹)

---- 一堆 .bin 文件

-- .xml 文件

-- .xml 文件


您正在使用一个ByteArrayOutputStream https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html对象作为下载的输出。如果您的程序在没有将该对象的内容保存在某处的情况下终止,您将无法在计算机磁盘中找到该信息,因为它没有写入磁盘,而是作为缓冲字节数组保存在内存中(请参阅上一个链接以获取更多信息)。

如果您想将下载的输出保存到文件中,我建议您使用FileOutputStream https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html作为下载的目的地。为此,您必须按如下方式修改代码:

  1. 添加适当的import宣言:

    import java.io.FileOutputStream;
    
  2. 修改你的outputStream变量赋值如下:

    OutputStream outputStream = new FileOutputStream('/tmp/downloadedfile');
    

    参数传递到哪里FileOutputStream应该是您下载所需的目标路径。

  3. 将任何内容写入文件后,添加以下代码行:
    outputStream.flush();
    outputStream.close();
    
    这将确保您的文件被正确写入。

关于下载文件夹,您是完全正确的 - 您首先需要获取要下载的文件夹及其每个子文件夹。为了更好地理解如何做到这一点,我建议您查看以下答案:使用 Google Drive API 下载文件夹 https://stackoverflow.com/questions/41184940/download-folder-with-google-drive-api

编辑 - 下载文件夹的示例

String destinationFolder = "/tmp/downloadedfiles/";
List<File> files = result.getFiles();
File newFile;
if (files == null || files.isEmpty()) {
  System.out.println("No files found.");
} else {
  System.out.println("Files:");
  for (File file : files) {
    System.out.printf("%s (%s)\n", file.getName(), file.getId());
    String fileId = file.getId();
    String fileName = file.getName();
    OutputStream outputstream = new FileOutputStream(destinationFolder + fileName);
    service.files().get(fileId)
           .executeMediaAndDownloadTo(outputstream);
    outputstream.flush();
    outputstream.close();
  }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Drive API 不下载文件 (Java v3) 的相关文章

随机推荐