Java Google drive rest API,仅从根文件夹下载文件

Java Google drive rest API,仅从根文件夹下载文件,java,google-api,google-drive-api,google-api-java-client,Java,Google Api,Google Drive Api,Google Api Java Client,我正在尝试只下载根目录中的文件。目前我没有指定任何文件夹,因为我不知道如何指定,所以它会下载其他非根目录文件夹中的最新文件。我想要的只是根目录中的文件。获取文件和下载URL的代码如下: public static void startDownload() throws IOException, ParseException { Drive serv = getDriveService(); FileList result = serv.files().list().setMa

我正在尝试只下载根目录中的文件。目前我没有指定任何文件夹,因为我不知道如何指定,所以它会下载其他非根目录文件夹中的最新文件。我想要的只是根目录中的文件。获取文件和下载URL的代码如下:

public static void startDownload() throws IOException, ParseException {

    Drive serv = getDriveService();

    FileList result = serv.files().list().setMaxResults(10).execute(); //there are 10 files in the root folder     

    List<File> listA = result.getItems();

    if (listA == null || listA.isEmpty()) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:"+lista.size());
        for (File file : listA) {
            System.out.printf("%s (%s)\n", file.getTitle(), file.getDownloadUrl());
            downloadFile(serv, file);
        }
    }
}
public static void startDownload()引发IOException,ParseException{
Drive serv=getDriveService();
FileList result=serv.files().list().setMaxResults(10.execute();//根文件夹中有10个文件
List listA=result.getItems();
if(listA==null | | listA.isEmpty()){
System.out.println(“未找到文件”);
}否则{
System.out.println(“文件:+lista.size());
for(文件:listA){
System.out.printf(“%s(%s)\n”、file.getTitle()、file.getDownloadUrl());
下载文件(serv,file);
}
}
}
我想只下载根文件中的所有文件,而不下载任何其他文件夹中的文件。任何帮助都将不胜感激。

您需要使用参数进行搜索

q字符串用于筛选文件结果的查询。请参阅“搜索 支持语法的“文件”指南

发送类似于以下内容的内容将返回所有不属于root父文件夹的内容

mimeType!='父目录中的application/vnd.google apps.folder'和'root'


有许多考虑因素

你的3号线需要

String q=“trashed=false,父级和mimeType中的“root!”=“application/vnd.google apps.folder”

FileList result=serv.files().list().setFields(“*”).setQ(q).setMaxResults(10).execute()

您需要意识到这将返回最多10个结果,但更重要的是,您需要意识到没有最小数量的结果。这意味着如果您有11个文件,可能在第一次迭代中得到10个,在第二次迭代中得到1个。但是,您也可以得到1和10,或者3和6和2,或者0和0以及1和10。您需要一直获取结果,直到
getnextpGetOken()==null
的值。那你的台词呢

if(listA==null | | listA.isEmpty()){

应该是

if(result.getNextPageToken()==null){

我知道你是从官方文件中复制/粘贴的,但遗憾的是文件是错误的