如何使用 Java Google Drive API 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39182741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:09:01  来源:igfitidea点击:

How to upload file using Java Google Drive API

javagoogle-drive-api

提问by tsaebeht

I would like to upload a file using Google Drive API. I have looked at Google Drive API for Javaand Google Drive API Javadoc, but I don't see anything.

我想使用 Google Drive API 上传文件。我看过Google Drive API for JavaGoogle Drive API Javadoc,但我什么也没看到。

回答by noogui

The Uploading Filesin Drive API that @Polyov gave you contains a Java code snippet:

@Polyov 为您提供的 Drive API 中的上传文件包含一个 Java 代码片段:

File fileMetadata = new File();
fileMetadata.setName("My Report");
fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");

java.io.File filePath = new java.io.File("files/report.csv");
FileContent mediaContent = new FileContent("text/csv", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
System.out.println("File ID: " + file.getId());

Whichever language you use, the concepts remain the same like there will always be the 3 types of uploading, namely: simple upload, multipart and resumable.

无论您使用哪种语言,概念都保持不变,就像总是有 3 种上传类型,即:简单上传、分段上传和可恢复上传。

There's a Java Quickstartto get you started.

有一个Java 快速入门可以帮助您入门。

If you're looking for more code samples check this github repofor your reference.

如果您正在寻找更多代码示例,请查看此github 存储库以供您参考。