2

所以我想创建一个java.io.File以便我可以使用它来生成一个多部分形式的 POST 请求。我有一个格式的文件,com.google.api.services.drive.model.File所以我想知道,有没有办法可以将此 Google 文件转换为 Java 文件?这是一个使用 Google App Engine SDK 的网络应用程序,它禁止我尝试使其工作的所有方法

4

2 回答 2

4

不,您似乎无法从 com.google.api.services.drive.model.File 转换为 java.io.File。但是仍然可以使用 Drive 中的数据生成多部分形式的 POST 请求。

因此 com.google.api.services.drive.model.File 类用于存储有关文件的元数据。它不存储文件内容。

如果您想将文件的内容读入内存,驱动器文档中的这段代码片段显示了如何执行此操作。一旦文件在内存中,你可以对它做任何你想做的事情。

 /**
 * Download the content of the given file.
 *
 * @param service Drive service to use for downloading.
 * @param file File metadata object whose content to download.
 * @return String representation of file content.  String is returned here
 *         because this app is setup for text/plain files.
 * @throws IOException Thrown if the request fails for whatever reason.
 */
private String downloadFileContent(Drive service, File file)
    throws IOException {
  GenericUrl url = new GenericUrl(file.getDownloadUrl());
  HttpResponse response = service.getRequestFactory().buildGetRequest(url)
      .execute();
  try {
    return new Scanner(response.getContent()).useDelimiter("\\A").next();
  } catch (java.util.NoSuchElementException e) {
    return "";
  }
}

https://developers.google.com/drive/examples/java

这篇文章可能有助于从 Google AppEngine 发出多部分 POST 请求。

于 2013-07-22T20:54:27.563 回答
0

在 GoogleDrive Api v3 中,您可以将文件内容下载到您的 OutputStream 中。您需要文件 id,您可以从com.google.api.services.drive.model.File获得:

String fileId = "yourFileId";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId).executeMediaAndDownloadTo(outputStream);
于 2019-09-24T17:36:34.587 回答