如何使用vfs2 StandardFileSystemManager创建sftp会话?

时间:2018-11-09 22:38:32

标签: java apache sftp apache-commons apache-commons-vfs

我想创建一个sftp会话,以使用apache commons vfs2下载文件。 我该怎么做?

我的代码如下。但是问题是manager.resolve继续打开连接。 我想进行5分钟的会话,并在空闲时将其关闭,并在需要时重新打开该会话。这可能吗?

public void download() {
   StandardFileSystemManager manager = new StandardFileSystemManager();
   FileSystemOptions fileSystemOptions = new FileSystemOptions();

   SftpFileSystemConfigBuilder.getInstance()
                                   .setStrictHostKeyChecking(fileSystemOptions, "no");
        SftpFileSystemConfigBuilder.getInstance()
                                   .setUserDirIsRoot(fileSystemOptions, true);
        SftpFileSystemConfigBuilder.getInstance()
                                   .setTimeout(fileSystemOptions, 10000);
        SftpFileSystemConfigBuilder.getInstance()
                                   .setPreferredAuthentications(fileSystemOptions, "password");

   try {
            manager.init();
            File f = new File("myfolder");
            final FileObject localFolder = manager.resolveFile(f.toURI());
            final FileObject remoteFile = manager.resolveFile(remoteURI, fileSystemOptions);

            localFolder.copyFrom(remoteFile, Selectors.SELECT_FILES);
    } finally {
            manager.freeUnusedResources();
            manager.close();
        }
}

0 个答案:

没有答案