在 sshj v0.5.0 中上传和下载文件时设置文件过滤器

发布于 2024-12-05 01:41:32 字数 914 浏览 0 评论 0原文

我一直在使用 sshj 库,

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

以下是我使用 0.3.1 的代码,它可以很好地上传支持通配符模式的文件。

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);
}

但现在,当我尝试转向 0.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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-12 01:41:32

目前使用 0.5.0,这不再可能。我为 SSHJ 的 Shikhar(维护者)创建了一个拉取请求

下面我修改了您的代码示例以使其与 0.5.0 一起工作。基本的变化是您现在需要向复制方法提供 LocalSourceFile。为了使 SCPUploadClient 能够仅发送目录的过滤内容,我重写了 getChildren(LocalFileFilter) 方法。

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);
    }

}

对于 WildcardFileFilter 请参阅我关于如何在 0.6.0 中执行此操作的第二个答案。

希望这有帮助。

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.

假情假意假温柔 2024-12-12 01:41:32

使用 0.6.0,您可以编写代码如下:

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);
}

我猜您使用的 WildcardFileFilter 来自 commons-io。由于这是一个 FilenameFilter 而不是 LocalFileFilter,因此您可以非常简单地将其实现为:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文