简体   繁体   English

java google drive API 无法下载二进制文件

[英]java google drive API unable to download binary file

I have working code to list the files in my Google Drive folder containing binary files but I'm getting an error when trying to download:我有工作代码来列出我的 Google Drive 文件夹中包含二进制文件的文件,但我在尝试下载时遇到错误:

GET https://www.googleapis.com/download/drive/v3/files/1udFizffA_0M4jSUBJbDI6uNVdPVhdymW?alt=media
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "location" : "alt",
    "locationType" : "parameter",
    "message" : "Only files with binary content can be downloaded. Use Export with Docs Editors files.",
    "reason" : "fileNotDownloadable"
  } ],
  "message" : "Only files with binary content can be downloaded. Use Export with Docs Editors files."
}

The relevant code is as follows:相关代码如下:

Drive.Files f = driveService.files();
HttpResponse httpResponse = null;
Drive.Files.Get get = f.get(file.getId());
httpResponse = get.executeMedia();

I also tried export using:我还尝试使用以下方式导出:

httpResponse = f.export(file.getId(),"application/octet-stream").executeMedia();

which didn't work either.这也没有用。

Can someone please help me figure out what I'm missing.有人可以帮我弄清楚我错过了什么吗?

The file.get method does return the file data, however only for binary file types. file.get方法确实返回文件数据,但仅适用于二进制文件类型。 that is what this error message its telling you.这就是此错误消息告诉您的内容。

Only files with binary content can be downloaded.只能下载包含二进制内容的文件。 Use Export with Docs Editors files.使用 Export with Docs Editors 文件。

String fileId = "0BwwA4oUTeiV1UVNwOHItT0xfa2M";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId)
    .executeMediaAndDownloadTo(outputStream);

All other file types those being google docs, google sheets.所有其他文件类型是谷歌文档、谷歌表格。 check this page for more: mimetype查看此页面了解更多信息: mimetype

Need to be exported from the google mime type to a standard mime type.需要从 google mime 类型导出为标准 mime 类型。 If you check the manage-downloads#java page you will find a documented example of how to use this method.如果您查看manage-downloads#java页面,您将找到有关如何使用此方法的文档示例。

String fileId = "1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().export(fileId, "application/pdf")
    .executeMediaAndDownloadTo(outputStream);

The trick with this method is to ensure you are setting the correct mimetype此方法的技巧是确保您设置正确的mimetype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM