2015-10-21 157 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

相關問題