请查看 Google Drive SDK文档中提供的 DrEdit Java 示例。此示例说明如何授权和构建读取元数据、文件数据和将内容上传到 Google Drive 的请求。
这是一个代码片段,展示了如何使用ByteArrayContent
将媒体上传到存储在字节数组中的 Google Drive:
/**
* Create a new file given a JSON representation, and return the JSON
* representation of the created file.
*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Drive service = getDriveService(req, resp);
ClientFile clientFile = new ClientFile(req.getReader());
File file = clientFile.toFile();
if (!clientFile.content.equals("")) {
file = service.files().insert(file,
ByteArrayContent.fromString(clientFile.mimeType, clientFile.content))
.execute();
} else {
file = service.files().insert(file).execute();
}
resp.setContentType(JSON_MIMETYPE);
resp.getWriter().print(new Gson().toJson(file.getId()).toString());
}