2017-06-21 123 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

相关问题