[英]Download File from Google Drive in Java
My end goal is to write a simple program that downloads one specific Google spreadsheet at the end of each week to my local hard drive. 我的最终目标是编写一个简单的程序,每星期结束时将一个特定的Google电子表格下载到我的本地硬盘中。 The file will be edited on a week to week basis by other people, and I will constantly need to download the updated version to pull out data and pass to a bash script that I have written.
该文件将由其他人每周进行一次编辑,我将不断需要下载更新的版本以提取数据并传递给我编写的bash脚本。 I am now doing this manually, but if I can pull down the spreadsheet in a csv format, can automate the entire process.
我现在是手动执行此操作,但是如果我可以使用csv格式下拉电子表格,则可以自动化整个过程。
I am slightly confused by Google's Documentation. Google的文档让我有些困惑。 I have copied their example method
downloadFile
, but am still unclear about the exact parameters that are being passed, and the InputStream
return. 我已经复制了他们的示例方法
downloadFile
,但是仍然不清楚传递的确切参数以及InputStream
返回。
If I were to add a main method to call the downloadFile
method, what would be example parameters that I pass. 如果要添加一个主方法来调用
downloadFile
方法,那么我传递的示例参数将是什么。
Then, can I use the InputPut
stream it returns to save the file is csv format? 然后,我可以使用它返回的
InputPut
流来保存csv格式的文件吗?
Also, is this the only method I need or do is there more to it? 另外,这是我唯一需要或做的唯一方法吗?
downloadFile
method is defined here (left out the import statements): 在这里定义了
downloadFile
方法(忽略了导入语句):
private static InputStream downloadFile (Drive service, File file) {
if (file.getDownloadUrl() !=null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
return resp.getContent();
}
catch (IOException e) {
e.printStackTrace();
return null;
}
}
else {
return null;
}
}
For the second parameter you need an instance of the File class. 对于第二个参数,您需要File类的实例。 From what I have found out.
从我所发现的。 Google's proprietory File class in it's simplest form can be instantiated with zero arguments.
最简单形式的Google专有File类可以用零参数实例化。 Like this.
像这样。
File f = new File();
To build a drive object there is an example on how to do it. 要构建驱动器对象,有一个有关如何执行驱动器的示例。 Scroll down to USE OAUTH 2.0 credentials.
向下滚动到USE OAUTH 2.0凭据。 https://developers.google.com/drive/credentials
https://developers.google.com/drive/credentials
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.