【问题标题】:Google drive export non-google doc file谷歌驱动器导出非谷歌文档文件
【发布时间】:2020-09-15 21:50:43
【问题描述】:

我正在尝试使用此代码从 Google 驱动器中导出多个文件:

String str = "application/rft";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, str).executeAndDownloadTo(outputStream);

它可以正常工作,但是当我尝试导出 application/pdfapplication/pngapplication/jpeg 文件时,我收到此错误:

403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Export only supports Google Docs.",
    "reason" : "fileNotExportable"
  } ],
  "message" : "Export only supports Google Docs."
}

我想知道是否有办法使用 Java 获取此文件的内容

提前致谢

【问题讨论】:

    标签: java google-drive-api


    【解决方案1】:

    Google Docs 可以使用files.export 下载。但是当它下载除Google Docs以外的文件(pdf、png和jpeg)时,请使用files().get()

    您可以在here查看详细信息。您还可以查看示例脚本。

    编辑:

    这是一个用于下载除 Google 文档之外的文件的示例脚本。

    String fileId = "### file ID ###";
    OutputStream out = new FileOutputStream("### output filename ###");
    driveService.files().get(fileId).executeMediaAndDownloadTo(out);
    

    【讨论】:

    • 我试过了,但它没有给我内容,只是文件数据,如 id、kind、mimeType...
    • out 包含 { "kind": "drive#file", "id": "0B2VR-92nUWb9RXJySXhGZUlkckU", "name": "10-7-09.pdf", "mimeType": "application/pdf" } ,仍然不是内容。无论如何,非常感谢
    • @Aldeguer 我写给我的答案的脚本在我的环境中运行良好。那么我可以看看你当前的脚本吗?
    • 它已经在工作了,只是忘记了executeMediaAndDownload中的“媒体”,非常感谢老兄!!
    • @Aldeguer 听到这个我松了一口气。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多