2017-06-21 122 views
0

我正在尋找一種上傳谷歌驅動器上下載鏈接文件的方法。如何上傳帶有鏈接到Google Drive REST API v2的文件

我在文檔中沒有找到解釋如何去做的東西。

編輯:

我發現了兩個備選方案:

  • 首先是輸出來自環路
  • 第二的createdFile是第一次迭代恢復該文件的id然後進行更新。

第一個解決方案似乎是最快的。例如,當我從一個雲到另一個雲進行文件複製/粘貼時,使用固體資源管理器會快得多。 必須有一個更好的解決辦法?`

@Override 
    protected String doInBackground(String... sUrl) { 
     File body = new File(); 
     body.setTitle("some name"); 
     String fileId = null; 
     File createdFile; 
     InputStream input = null; 
     ByteArrayOutputStream bos = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.connect(); 

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      // download the file 
      input = connection.getInputStream(); 
      bos = new ByteArrayOutputStream(); 

      byte data[] = new byte[4096]; 
      total = 0; 
      int count; 
      while ((count = input.read(data)) != -1) { 
       // allow canceling with back button 
       if (isCancelled()) { 
        input.close(); 
        return null; 
       } 
       total += count; 

       bos.write(data, 0, count); 
       //option 2 
       if (fileId == null){ 
        createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute(); 
        fileId = createdFile.getId(); 

       }else{ 
        createdFile = mService.files().update(fileId, createdFile, new ByteArrayContent("", bos.toByteArray())).execute(); 
       } 

      } 
      //option 1 
      createdFile = mService.files().insert(body, new ByteArrayContent("", bos.toByteArray())).execute(); 
     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (bos != null) 
        bos.close(); 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 
    .............. 

你能告訴我一個解決方案嗎?

回答

0

最後我繼續像這樣:

@Override 
    protected String doInBackground(String... sUrl) { 

     com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); 
     body.setTitle("some name"); 
     InputStream input = null; 
     InputStreamContent mediaContent = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(sUrl[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      if (cookies != null){ 
       connection.setRequestProperty("Cookie", cookies); 
       connection.setRequestProperty("User-Agent", agent); 
       connection.setRequestProperty("Accept", "text/html, application/xhtml+xml, *" + "/" + "*"); 
       connection.setRequestProperty("Accept-Language", "en-US,en;q=0.7,he;q=0.3"); 
       connection.setRequestProperty("Referer", sUrl[0]); 
      } 
      connection.connect(); 

      if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
       return "Server returned HTTP " + connection.getResponseCode() 
         + " " + connection.getResponseMessage(); 
      } 

      input = connection.getInputStream(); 

      mediaContent = new InputStreamContent("", new BufferedInputStream(connection.getInputStream()) { 
       @Override 
       public int read() throws IOException { 

        return 0; 
       } 
       @Override 
       public int read(byte[] buffer, 
           int byteOffset, int byteCount) 
         throws IOException { 

        return super.read(buffer, byteOffset, byteCount); 
       } 
      }); 

      com.google.api.services.drive.model.File f = mService.files().insert(body, mediaContent).execute(); 

     } catch (Exception e) { 
      return e.toString(); 
     } finally { 
      try { 
       if (input != null) 
        input.close(); 
      } catch (IOException ignored) { 
      } 

      if (connection != null) 
       connection.disconnect(); 
     } 
     return null; 
    } 

我希望這將有助於。

1

您不能設置任意的唯一ID。你有兩個選擇

  1. 給它留空。 最常見的機制是根本不設置任何ID。當您創建文件時,Google Drive會創建一個ID並返回createdFile.id

  2. 請求ID的緩存。 您可以使用https://developers.google.com/drive/v2/reference/files/generateIds來獲取一些ID,然後保留給您使用。您可以在您的body.id中設置其中一個ID。

但是,我懷疑你真正的問題是你還沒有理解文件ID與文件名的重要性。在Google雲端硬盤中,文件由其ID標識,而不是其名稱。文件名只是文件的一個屬性,就像修改日期和MIME類型一樣。如果您創建了10個文件,您將獲得10個文件。如果這些創建的所有文件都具有相同的名稱,則猜測這10個文件中的每一個文件都將具有相同的名稱。

如果您打算更新相同的文件,而不是創建一個新文件,那麼您應該使用https://developers.google.com/drive/v2/reference/files/update而不是創建。

+0

我找到了兩個替代方案: - 第一個是從循環 輸出createdFile - 第二個是第一次迭代恢復文件的id然後進行更新。 第一個解決方案似乎是最快的。例如,當我從一個雲到另一個雲進行文件複製/粘貼時,使用固體資源管理器會快得多。 必須有更好的解決方案嗎? – Aristide13

相關問題