无法使用Java API从Google云端硬盘下载文件
问题描述:
我尝试了Google云端硬盘的某些功能,但无法从云端硬盘下载文件。我是否缺少一些功能来创建文件?无法使用Java API从Google云端硬盘下载文件
private static InputStream downloadFile(Drive authKey, File file) {
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
try {
HttpResponse resp
= authKey.getRequestFactory().buildGetRequest(new GenericUrl(
file.getDownloadUrl())).execute();
//System.out.println("File Successfully Downloaded");
return resp.getContent();
} catch (IOException e) {
// An error occurred.
e.printStackTrace();
//System.out.println("Failed to Download File");
return null;
}
} else {
return null;
}
}
当我运行的功能,它成功地建立。但我没有看到任何东西,例如下载的文件等。
答
这是我在Android上使用的构造,无论如何您都可以尝试使用它。
private static InputStream downloadFile(Drive authKey, File file) {
if (authKey != null && file != null) try {
File gFl = authKey.files().get(file.getId()).setFields("downloadUrl").execute();
if (gFl != null){
return authKey.getRequestFactory()
.buildGetRequest(new GenericUrl(gFl.getDownloadUrl()))
.execute().getContent();
}
} catch (Exception e) { e.printStackTrace(); }
return null;
}
(虽然效率不高,2个往返)
UPDATE
我花了一些时间,并通过我的Android调试/测试应用程序运行你的代码,并可以确认失败。
private static InputStream downloadFile(Drive authKey, File file) {
// if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) try {
// return authKey.getRequestFactory()
// .buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute().getContent();
// } catch (IOException e) { e.printStackTrace(); }
// return null;
if (authKey != null && file != null) try {
File gFl = authKey.files().get(file.getId()).setFields("downloadUrl").execute();
if (gFl != null){
return mGOOSvc.getRequestFactory()
.buildGetRequest(new GenericUrl(gFl.getDownloadUrl()))
.execute().getContent();
}
} catch (Exception e) { e.printStackTrace(); }
return null;
}
上面代码的注释部分是失败的变体。
祝你好运
你有什么错误吗?你想下载什么样的文件? – Gerardo
你正在处理什么系统? (我可以帮助Android) – seanpj
@Gerardo没有任何错误。 我试图下载“.part1”文件,我以前分裂.. – septo13