使用Java API从Google云端硬盘下载文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Java API从Google云端硬盘下载文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2018字,纯文字阅读大概需要3分钟。
内容图文
![使用Java API从Google云端硬盘下载文件](/upload/InfoBanner/zyjiaocheng/697/52db9c9a3df04757a0f91716c9c32a13.jpg)
我正在使用Java API来创建应用程序.但是,我无法解决从驱动器下载文件的问题.
我正在尝试使用Google Developer’s页面上提供的功能. (下方链接)
https://developers.google.com/drive/manage-downloads
但是,没有明确提到如何为特定文件获取/生成downloadURI.而且我也很困惑如何使用downloadURI下载文件.
我使用以下功能 –
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;
}
}
在这里,我无法获得该文件应该是该函数的输入参数.
请帮帮我.
解决方法:
这就是我做到的.
我没有下载文档,而是下载给定文档的所有修订版.所以逻辑是一样的.
private void downloadRevisions(final RevisionList revisions,
final Drive service) {
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
//TODO for debugging
int i = 0;
for (Revision revision : revisions.getItems()) {
System.out.println("Thread running: " + i);
if (revision.getExportLinks() != null
&& revision.getExportLinks().get("text/plain") != null
&& revision.getExportLinks().get("text/plain").length() > 0) {
HttpResponse resp = service
.getRequestFactory()
.buildGetRequest(
new GenericUrl(revision
.getExportLinks().get("text/plain")))
.execute();
//InputStream inputStream = resp.getContent();
//File Name
String revisionFileName = docId+"_"+revision.getId()+"_"+revision.getModifiedDate();
FileOutputStream outputstream = new FileOutputStream(new java.io.File(revisionFileDir, revisionFileName));
IOUtils.copy(resp.getContent(), outputstream,true);
outputstream.close();
// writeToFile(inputStream);
System.out.println("downloading: " + revisionFileName);
}
//TODO for debugging
i++;
}
System.out.println("Downloading Done. Document Id: "+docId);
} catch (IOException e) {
Log.info("WriteToFile Fail", e.toString());
e.printStackTrace();
}finally{
//if( doneFW !=null)
// doneFW.close();
}
}
});
thread.start();
}
内容总结
以上是互联网集市为您收集整理的使用Java API从Google云端硬盘下载文件全部内容,希望文章能够帮你解决使用Java API从Google云端硬盘下载文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。