开发者

Setting File filters when doing uploading and downloading of files in sshj v0.5.0

I have been using sshj libraries

<dependency>
    <groupId>net.schmizz</groupId>
    <artifactId>sshj</artifactId>
    <version>0.3.1</version>
</dependency>

Following was my code using 0.3.1 which worked fine for doing uploading of files supporting wildcard patterns.

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.开发者_开发百科newSCPFileTransfer().newSCPUploadClient();
    uploader.setFileFilter(new WildcardFileFilter(wildCardPattern));

    //determine the remote directory
    File f = new File(localDirLocation);
    String dir = remoteDirLocation + f.getName();
    uploader.copy(localDirLocation, remoteDirLocation);
} catch (IOException e) {
      //processing exceptions here
} finally {
    disconnectClient(client);
}

But now the code gives me a lot of compilation errors when I tried moving over to 0.5.0.

I would like to understand how do I go about setting fileFilters when I want to do uploading and downloading of files from local to remote machine and vice versa

Can someone please help me with this ?


Currently using 0.5.0 this is no longer possible. I created a pull request for Shikhar (the maintainer) of SSHJ.

Below I've adapted your code sample to make it work with 0.5.0. The basic change is that you now need to provide a LocalSourceFile to the copy method. In order to make it possible for the SCPUploadClient to only send the filtered contents of the directory, I've overridden the getChildren(LocalFileFilter) method.

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.newSCPFileTransfer().newSCPUploadClient();
    File f = new File(localDirLocation);
    FilteredFileSystemDirectory filteredDir = new FilteredFileSystemDirectory(f, new WildcardFileFilter());
    String dir = remoteDirLocation + f.getName();
    uploader.copy(filteredDir, remoteDirLocation);
} catch (IOException e) {
    //processing exceptions here
} finally {
    disconnectClient(client);
}

public class FilteredFileSystemDirectory extends FileSystemFile {
    private final LocalFileFilter filter;

    public FilteredFileSystemDirectory(File f, LocalFileFilter filter) {
        super(f);
        this.filter = filter;
    }

    @Override
    public Iterable<? extends LocalSourceFile> getChildren(LocalFileFilter filter)
        throws IOException {
        return super.getChildren(filter);
    }

}

For the WildcardFileFilter see my second answer on how to do this in 0.6.0.

Hope this helps.


Using 0.6.0 you can write your code as follows:

SSHClient client = null;
SCPUploadClient uploader = null;
try {
    client = getClient();
    uploader = client.newSCPFileTransfer().newSCPUploadClient();
    uploader.setUploadFilter(new WildcardFileFilter(wildCardPattern));

    //determine the remote directory
    File f = new File(localDirLocation);
    String dir = remoteDirLocation + f.getName();
    uploader.copy(new FileSystemFile(localDirLocation), remoteDirLocation);
} catch (IOException e) {
    //processing exceptions here
} finally {
    disconnectClient(client);
}

The WildcardFileFilter you used came from commons-io I guess. As that is a FilenameFilter and not a LocalFileFilter, you could very simply implement this as:

public WildcardFileFilter implements LocalFileFilter {
    private String wildcardPattern;

    public WildcardFileFilter(String wildcardPattern) {
        this.wildcardPattern = wildcardPattern;
    }

    @Override
    public boolean accept(LocalSourceFile file) {
        return FilenameUtils.wildcardMatchOnSystem(file.getName(), wildcardPattern);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜