Insert file using Google Drive API?

707 Views Asked by At

Based on Google Drive API reference guiding how to insert file Files.insert

// File's metadata.
File body = new File();
body.setTitle(title);
body.setDescription(description);
body.setMimeType(mimeType);

// File's content.
java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent(mimeType, fileContent);
try {
  File file = service.files().insert(body, mediaContent).execute();

  // Uncomment the following line to print the File ID.
  // System.out.println("File ID: " + file.getId());

  return file;
} catch (IOException e) {
  System.out.println("An error occured: " + e);
  return null;
}

However, my application is web application and it does not support java.io.File package when uploaded to Google app engine => I can not use this to set content for file object

java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent(mimeType, fileContent);

Is there any other methods to implement this such as using blob or binary data to be file content instead?

1

There are 1 best solutions below

1
On BEST ANSWER

Instead of FileContent, use one of the other subclasses of AbstractInputStreamContent such as ByteArrayContent