2

So I want to create a java.io.File so that I can use it to generate a multipart-form POST request. I have the file in the form of a com.google.api.services.drive.model.File so I'm wondering, is there a way I can convert this Google File to a Java File? This is a web-app that uses the Google App Engine SDK, which prohibits every approach I've tried to make this work

Dangerbunny
  • 81
  • 1
  • 3
  • 10

2 Answers2

4

No, you it doesn't seem like you can convert from com.google.api.services.drive.model.File to java.io.File. But it should still be possible to generate a multipart-form POST request using your data in Drive.

So the com.google.api.services.drive.model.File class is used for storing metadata about the file. It's not storing the file contents.

If you want to read the contents of your file into memory, this code snippet from the Drive documentation shows how to do it. Once the file is in memory, you can do whatever you want with it.

 /**
 * 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

This post might be helpful for making your multi-part POST request from Google AppEngine.

Community
  • 1
  • 1
Robert Parker
  • 605
  • 4
  • 7
  • Right, the above code is basically what I'm using to get the raw string file data. But I can't figure out how to re-create a java.io.File object using that String in a way GAE allows, and I need a java.io.File object in order to create a FileBody object, which will be attached to the multipart-form post. Checking out that other stack overflow link you posted – Dangerbunny Jul 22 '13 at 21:35
  • The other link relies on the file being stored in the GAE database. Do you think this would the only way to get this whole thing to work? – Dangerbunny Jul 22 '13 at 21:46
  • It's not entirely a copy/paste solution. You have a file stored in Google drive, and you want to send that via multipart POST request. The GAE documentation shows how to read the file from Google Drive, the stack overflow example shows how to send a multipart POST request. Edit those two things together, and you have the solution. You will probably need to change some things around. – Robert Parker Jul 23 '13 at 16:03
0

In GoogleDrive Api v3 you can download the file content into your OutputStream. You need for that the file id, which you can get from your com.google.api.services.drive.model.File:

String fileId = "yourFileId";
OutputStream outputStream = new ByteArrayOutputStream();
driveService.files().get(fileId).executeMediaAndDownloadTo(outputStream);
Andrew
  • 2,438
  • 1
  • 22
  • 35