如何使用java在Google云端硬盘中获取子级的文件夹名称

时间:2016-10-20 08:25:42

标签: java google-api google-drive-api google-api-java-client

我正在使用java中的Google Drive API。我的代码中有孩子的文件夹ID,但我想要孩子的文件夹名称。我应用所有方法来获取儿童的文件夹名称,但我没有得到

Drive.Children.List FolderID = service.children().list(child.getId());

从这段代码我得到的文件夹ID就像0B3-sXIe4DGz1c3RhcnRlcl9。

 Drive.Children.List Foldername = service.children().list(child.getId().getClass().getName());

在此代码中,它返回{folderId = java.lang.String}

如何获取文件夹的名称?

1 个答案:

答案 0 :(得分:0)

在Google云端硬盘文件夹中是文件。您有使用Files.get的文件夹ID来返回包含该文件夹标题的FileResource

Files.get

的文档中删除了代码
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpResponse;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import java.io.IOException;
import java.io.InputStream;

// ...

public class MyClass {

  // ...

  /**
   * Print a file's metadata.
   *
   * @param service Drive API service instance.
   * @param fileId ID of the file to print metadata for.
   */
  private static void printFile(Drive service, String fileId) {

    try {
      File file = service.files().get(fileId).execute();

      System.out.println("Title: " + file.getTitle());
      System.out.println("Description: " + file.getDescription());
      System.out.println("MIME type: " + file.getMimeType());
    } catch (IOException e) {
      System.out.println("An error occured: " + e);
    }
  }

  /**
   * Download a file's content.
   *
   * @param service Drive API service instance.
   * @param file Drive File instance.
   * @return InputStream containing the file's content if successful,
   *         {@code null} otherwise.
   */
  private static InputStream downloadFile(Drive service, File file) {
    if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) {
      try {
        HttpResponse resp =
            service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl()))
                .execute();
        return resp.getContent();
      } catch (IOException e) {
        // An error occurred.
        e.printStackTrace();
        return null;
      }
    } else {
      // The file doesn't have any content stored on Drive.
      return null;
    }
  }

  // ...
}