Dropbox API列表文件夹和文件夹递归

时间:2016-08-10 11:36:51

标签: dropbox-api

如何列出文件夹层次结构并删除Dropbox帐户中的文件? 下面的方法仅列出根目录的文件夹。任何人都知道图书馆会告诉你是否准备好了这样的功能吗?

    ListFolderResult result;
    try {
        result = client.files().listFolder(StringUtils.EMPTY);

        while (true) {
            for (Metadata metadata : result.getEntries()) {
                System.out.println(metadata.getPathLower());
            }

            if (!result.getHasMore()) {
                break;
            }
            result = client.files().listFolderContinue(result.getCursor());
        }
    } catch (DbxException exception) {
        System.out.println(exception.toString());
    }

4 个答案:

答案 0 :(得分:0)

默认情况下,listFolder不是递归的。

对于递归列表,您应该使用ListFolderBuilder并使用withRecursive将递归设置为true

答案 1 :(得分:0)

我通过以下代码得到了我需要的东西。谢谢!

public void listFolderAndFiles() {

    try {
        ListFolderResult result = client.files().listFolderBuilder("").withIncludeDeleted(false).withRecursive(true).withIncludeMediaInfo(true)
                .start();

        // while (true) {
        List<Metadata> entries = result.getEntries();
        int idx = 0;

        for (Metadata metadata : entries) {
            if (metadata instanceof FolderMetadata) {
                System.out.println("" + ++idx + ": FOLDER = " + metadata.getPathDisplay());

            } else if (metadata instanceof FileMetadata) {
                System.out.println("" + ++idx + ": File = " + metadata.getPathDisplay());
            }

            // String filePath = metadata.getPathLower().replace(metadata.getName().toLowerCase(), "");
            // System.out.println(metadata.getPathLower());
            // System.out.println("FILE PATH" + filePath);
            // System.out.println("Dropbox" + ((FileMetadata) metadata).getRev());
            // System.out.println("Dropbox" + ((FileMetadata) metadata).getClientModified());
            // System.out.println("Dropbox" + ((FileMetadata) metadata).getMediaInfo());
            // System.out.println("Dropbox" + ((FileMetadata) metadata).getMediaInfo().getMetadataValue());
            // System.out.println("Dropbox" + ((FileMetadata) metadata).getSharingInfo());
        }
        // }

    } catch (DbxException exception) {
        throw new BusinessException("Não foi possivel obter dados do diretório.");
    }

}

答案 2 :(得分:0)

@ Jeremias的解决方案有效,但您可能还想检查返回的ListFolder中是否有更多待处理的结果。每个查询有一个10,000项的上限。

while(true){  

    ListFolderResult result = client.files().listFolderBuilder(path)  
         .withRecursive(true) 
         .start();

    List<Metadata> entries = result.getEntries(); 
    for (Metadata metadata : entries) { ... do your logic here }

    if(result.getHasMore()){
         result = client.files().listFolderContinue(result.getCursor);
    } else {
         break; //no more entries to process
    }


}

还记得做try-catch。

答案 3 :(得分:0)

使用Compilation succeeded - 1 warning(s) jdoodle.cs(5,11): warning CS0414: The private field `check.checkobject' is assigned but its value is never used Stack overflow: IP: 0x5647646e1705, fault addr: 0x7fffc422eff8 Stacktrace: at <unknown> <0xffffffff> at (wrapper alloc) object.AllocSmall (intptr,intptr) <0x00103> <...> at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 ........ ........ ........ ........ ........ at check..ctor () [0x00000] in <db1fd2bd96e041fab014c4ec28898e03>:0 output Limit reached. 代替listFolderBuilder(path),如下所示:

listFolder