无法使用Java API从Google Drive下载文件 [英] Unable to download file from Google Drive using Java API
问题描述
我尝试了Google驱动器的某些功能,但无法从Drive下载该文件。我缺少一些函数来创建文件?
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){
//发生错误。
e.printStackTrace();
//System.out.println(\"Failed Download File);
返回null;
}
} else {
return null;
$ / code $ / pre
当我运行这个函数时,它会成功建立。但我没有看到任何东西,例如下载的文件等等。
解决方案
这是我在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(); }
返回null;
$ / code>
(尽管效率很低,但往返两次)
更新
我花了一些时间并通过我的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() 。))执行()的getContent();
//} catch(IOException e){e.printStackTrace(); }
//返回null; (authKey!= null&& file!= null)尝试{
文件gFl = authKey.files().get(file.getId())。setFields( downloadUrl)执行();
if(gFl!= null){
return mGOOSvc.getRequestFactory()
.buildGetRequest(new GenericUrl(gFl.getDownloadUrl()))
.execute()。getContent( );
}
} catch(Exception e){e.printStackTrace(); }
返回null;
}
上面代码的注释部分是失败的变体。
好运
I have tried some functions of Google drive, but I couldn't able to download the file from Drive. Am i missing some function to create the file?
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;
}
}
When i run the function, it build successfully. but i don't see anything, such a downloaded file or etc.
This is the construct I use on Android, you may try to give it a shot anyway.
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;
}
(inefficient though, 2 roundtrips)
UPDATE
I took some time and ran your code through my Android debug/test app and can confirm that it failed.
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;
}
The commented-out section of the code above is the failing variety.
Good Luck
这篇关于无法使用Java API从Google Drive下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!