如何从Java中的Google Drive API获取JSON中所有文件夹中的所有文件

时间:2017-08-18 07:41:53

标签: java json google-drive-api

是否有机会使用Google Drive API获取JSON中所有文件夹中的所有文件,只需简单发送GET方法即可?

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。您可以使用files.list。 这是相应的GET请求,默认返回所有文件。

GET https://www.googleapis.com/drive/v2/files

仔细看看here。还有一个在线演示来测试它。

此请求的Java代码可能如下所示:

private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    return result;
  }