2015-10-21 160 views
0

我尝试了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; 
    } 
} 

当我运行的功能,它成功地建立。但我没有看到任何东西,例如下载的文件等。

+0

你有什么错误吗?你想下载什么样的文件? – Gerardo

+0

你正在处理什么系统? (我可以帮助Android) – seanpj

+0

@Gerardo没有任何错误。 我试图下载“.part1”文件,我以前分裂.. – septo13

回答

0

这是我在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; 
    } 

上面代码的注释部分是失败的变体。

祝你好运

+0

谢谢你的帮助,但我没有看到代码运行很好.. 它建立成功。但我仍然没有看到任何东西下载:( – septo13

+0

唯一的另一个想法是一步一步地在这个[页](https://developers.google.com/drive/)底部的TryIt! v2/reference/files/get)(传递ID - file.getId() - 并检查结果)。 – seanpj

相关问题