0

为了使用网络共享文件夹中的一些文件,我将迁移到 JCIFS。到目前为止,我已经做了以下测试来列出我需要的文件(取自这个例子

public class ListDirectoryContent extends DosFileFilter {

    int count = 0;

    public ListDirectoryContent() {
        super("*.txt", 0xFFFF);
    }
    public ListDirectoryContent(String wildcard, int attributes) {
        super(wildcard, attributes);
    }

    public boolean accept(SmbFile file) throws SmbException {
        System.out.print( " " + file.getName() );
        count++;

        return (file.getAttributes() & attributes) != 0;
    }

    public static void main( String[] argv ) throws Exception {

        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("DOMAIN","myuser","A good password, yes sir!");
        SmbFile file = new SmbFile( "smb://networkResource/sharedFolder/Files/SubFolderOne/", auth);
        ListDirectoryContent llf = new ListDirectoryContent();

        long t1 = System.currentTimeMillis();
        SmbFile[] pepe = file.listFiles(llf);
        long t2 = System.currentTimeMillis() - t1;

        System.out.println();
        System.out.println( llf.count + " files in " + t2 + "ms" );

        System.out.println();
        System.out.println( pepe.length + " SmbFiles in " + t2 + "ms" );
    }
}

到目前为止,它适用于一个扩展通配符。如何扩大 dosfilefilter 以检查一组扩展?(就像 commons.io.FileUtils 一样)

4

1 回答 1

3

我编写了这个基本的 SmbFilenameFilter 以在文件名中使用通配符。希望这可以帮助!

private static class WildcardFilenameFilter implements SmbFilenameFilter {
    private static final String DEFAULT_WIDLCARD = "*";

    private final String wildcard = DEFAULT_WIDLCARD;
    private final String regex;

    public WildcardFilenameFilter(String filename) {
        regex = createRegexPattern(filename);
    }

    @Override
    public boolean accept(SmbFile dir, String name) throws SmbException {
        return name.matches(regex);
    }

    private String createRegexPattern(String filename) {
        return filename.replace(".", "\\.").replace(wildcard, ".+");
    }
}
于 2013-06-18T12:13:05.863 回答