1

当我提供文件夹 ID 或 MIME 类型时,Google Drive 没有列出文件夹中的文件。它适用于具有空查询的根文件夹中的文件。如何列出文件夹中的文件?

代码:

public ArrayList<String> getDriveList(String folder, String folderId) throws IOException {
    //  Create a new authorized API client
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();
    Credential credential = Utils.getCredential();
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential)
        .setApplicationName("SDM Mailer App")
        .build();
    ArrayList<File> result = new ArrayList<File>();
//      String query = "";
    String query = "'"+folderId+"' in parents";
//      String query = "mimeType = 'application/vnd.ms-excel'";
//      String query = "'"+folderId+"' in parents and mimeType = 'application/vnd.ms-excel'";
    log.info("Finding files with query: " + query);
    Drive.Files.List request = service.files().list().setQ(query);

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

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

    log.info("Found # results: " + result.size());
    ArrayList<String> resultPlain = new ArrayList<String>();
    for (File f: result) {
        resultPlain.add(f.getTitle());
    }
    return resultPlain;

} // driveList

日志:

INFO: Finding files with query: '0B72jLeZWY3rsQllNMWhCQWhtLWc' in parents
Oct 04, 2013 8:50:42 PM com.example.server.SDMServiceImpl getDriveList
INFO: Found # results: 0

或使用空查询,根文件夹:

INFO: Finding files with query: 
Oct 04, 2013 9:20:05 PM com.example.server.SDMServiceImpl getDriveList
INFO: Found # results: 2

驱动器文件:

伊姆古尔

参考文档:

https://google-developers.appspot.com/drive/search-parameters

罐:

google-api-services-drive-v2-rev102-1.16.0-rc.jar

4

0 回答 0