无法使用 Google API Java 下载 Google 工作场所文档 GoogleSheet 到本地

Cannot download Google workplace document like GoogleSheet to local using Google API Java

作为 Google Drive API 文件导出 Google Sheet 存储在 GG Drive: https://developers.google.com/drive/api/v3/manage-downloads https://developers.google.com/drive/api/v3/ref-export-formats

为了下载 GoogleSheet:

我写了下面的 Java 方法
public void downloadFileFromDrive() throws Exception {
        String fileId = "1KVNuObZ4ACxKuOFTjf-SKkIgsDAr4L0x5hJzDz_uzhU";
        OutputStream outputStream = new ByteArrayOutputStream();
        serviceDrive().files().export(fileId, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                .executeMediaAndDownloadTo(outputStream);
    }

我运行程序没有错误。我的问题:

  1. Excel 文件保存在哪里?该文档没有说明文件保存在本地的目标文件夹。我搜索了我 PC 中的所有文件,但没有看到我期望的任何文件

  2. 我是否需要声明从云端硬盘保存文件的目标文件夹路径?

正如@Federico 所说,它已经在里面 outputStream。但我建议使用 FileOutputStream,因为您要将其输出到文件。

样本:

OutputStream outputStream = new FileOutputStream(destinationFolder + fileName);
serviceDrive.files().export(fileId, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet").executeMediaAndDownloadTo(outputStream);
outputStream.flush();
outputStream.close();

参考: