成都个人学做网站,西安中高风险地区查询,合肥网站建设费用,模板网页制作几周前#xff0c;我评估了一些用于Java的SSH库。 对它们的主要要求是在远程计算机上进行文件传输和文件操作。 因此#xff0c;它存在一个基于SSH#xff0c;SSH文件传输协议#xff08;或SFTP#xff09;的网络协议。 因此#xff0c;我需要一个支持SFTP的SSH库。 一项… 几周前我评估了一些用于Java的SSH库。 对它们的主要要求是在远程计算机上进行文件传输和文件操作。 因此它存在一个基于SSHSSH文件传输协议或SFTP的网络协议。 因此我需要一个支持SFTP的SSH库。 一项研究表明它退出了许多用于Java的SSH库。 为了进行比较我将库的数量减少到三个。 我选择JSchSSHJ和Apache的Commons VFS以获得更深入的了解。 它们都支持SFTP。 JSch似乎是Java的实际标准。 SSHJ是较新的库。 其目标是为SSH提供一个清晰的Java API。 它在后台使用Apache SSHD。 Commons VFS的目标是为虚拟文件系统提供清晰的APISFTP是受支持的协议之一。 在后台它将JSch用于SFTP协议。 这些库应满足以下要求 通过密码进行客户端身份验证 通过公钥进行客户端身份验证 服务器认证 通过SFTP从本地主机上传文件 通过SFTP将文件下载到本地主机 远程主机上的文件操作例如通过SFTP移动删除列出给定文件夹的所有子文件夹在文件或文件夹等类型之后进行过滤 执行普通的shell命令 让我们更深入地了解这三个库如何满足需求。 客户端认证 这三个库都支持两种必需的身份验证方法。 SSHJ具有最清晰的身份验证API SSHClient.authUserPassSSHClient.authUserPublicKey 。 SSHClient sshClient new SSHClient();
sshClient.connect(host);// only for public key authentication
sshClient.authPublickey(user, location to private key file);// only for password authentication
sshClient.authPassword(user, password); 在Commons VFS中身份验证配置取决于应使用哪种身份验证。 对于公共密钥身份验证必须在FileSystemOption中设置私有密钥并且用户名是连接URL的一部分。 对于密码验证用户名和密码是连接URL的一部分。 StandardFileSystemManager fileSystemManager new StandardFileSystemManager();
fileSystemManager.init();// only for public key authentication
SftpFileSystemConfigBuilder sftpConfigBuilder SftpFileSystemConfigBuilder.getInstance();
FileSystemOptions opts new FileSystemOptions();
sftpConfigBuilder.setIdentities(opts, new File[]{privateKey.toFile()});
String connectionUrl String.format(sftp://%s%s, user, host);// only for password authentication
String connectionUrl String.format(sftp://%s:%s%s, user, password, host);// Connection set-up
FileObject remoteRootDirectory fileSystemManager.resolveFile(connectionUrl, connectionOptions); JSch中的认证配置类似于Commons VFS。 这取决于应使用哪种身份验证。 必须在JSch对象中配置用于公共密钥认证的私钥并且必须在Session对象中设置用于密码认证的密码。 对于两者当JSch对象获取Session对象时将设置用户名。 JSch sshClient new JSch();// only for public key authentication
sshClient.addIdentity(location to private key file);session sshClient.getSession(user, host);// only for password authentication
session.setPassword(password);session.connect();服务器认证 这三个库均支持服务器身份验证。 在SSHJ中可以使用SSHClient.loadKnownHost启用服务器身份验证。 可以添加自己的known_host文件位置也可以使用默认位置该位置取决于使用平台。 SSHClient sshClient new SSHClient();
sshClient.loadKnownHosts(); // or sshClient.loadKnownHosts(knownHosts.toFile());
sshClient.connect(host); 在Commons VFS中服务器身份验证配置也是FileSystemOption的一部分类似于公钥身份验证。 在那里可以设置known_hosts文件的位置。 SftpFileSystemConfigBuilder sftpConfigBuilder SftpFileSystemConfigBuilder.getInstance();
FileSystemOptions opts new FileSystemOptions();
sftpConfigBuilder.setKnownHosts(opts, new File(location of the known_hosts file)); 在JSch中存在两种配置服务器身份验证的可能性。 一种可能性是使用OpenSSHConfig 请参阅JSch示例以了解OpenSSHConfig 。 另一种可能性更容易。 可以直接在JSch对象中设置known_hosts文件的位置。 JSch sshClient new JSch();
sshClient.setKnownHosts(location of known-hosts file);通过SFTP上传/下载文件 这三个库均支持通过SFTP上传和下载文件。 SSHJ具有非常清晰的API用于这些操作。 SSHClient对象创建一个SFTPClient对象。 这个对象负责上传SFTPClient。PUT和下载SFTPClient。 获得 。 SSHClient sshClient new SSHClient();
// ... connectiontry (SFTPClient sftpClient sshClient.newSFTPClient()) {// downloadsftpClient.get(remotePath, new FileSystemFile(local.toFile()));// uploadsftpClient.put(new FileSystemFile(local.toFile()), remotePath);
} 在Commons VFS中上传和下载文件被抽象为在文件系统上的操作。 因此两者均由FileObject对象的copyFrom方法表示。 上传是参数】remotefile对象上的copyfrom操作和下载是在LOCALFILE一个的copyfrom操作。 StandardFileSystemManager fileSystemManager new StandardFileSystemManager();
// ... configuration
remoteRootDirectory fileSystemManager.resolveFile(connectionUrl, connectionOptions);LocalFile localFileObject (LocalFile) fileSystemManager.resolveFile(local.toUri().toString());
FileObject remoteFileObject remoteRootDirectory.resolveFile(remotePath);
try {// downloadlocalFileObject.copyFrom(remoteFileObject, new AllFileSelector());// uploadremoteFileObject.copyFrom(localFileObject, new AllFileSelector());
} finally {localFileObject.close();remoteFileObject.close();
} JSch还支持SFTPClient。 在JSch中它称为ChannelSFTP 。 它有两种下载 ChannelSFTP.get 和上载 ChannelSFTP.put 的方法。 // here: creation and configuration of sessionChannelSftp sftpChannel null;
try {sftpChannel (ChannelSftp) session.openChannel(sftp);sftpChannel.connect();// downloadInputStream inputStream sftpChannel.get(remotePath);Files.copy(inputStream, localPath);// uploadOutputStream outputStream sftpChannel.put(remotePath);Files.copy(locaPathl, outputStream);
} catch (SftpException | JSchException ex) {throw new IOException(ex);
} finally {if (sftpChannel ! null) {sftpChannel.disconnect();}
}执行Shell命令 仅Commons VFS不支持执行普通Shell命令。 在SSHJ中它是两层的。 SshClient启动一个新的Session对象。 该对象执行shell命令。 这是非常直观的。 // creation and configuration of sshClienttry (Session session sshClient.startSession()) {session.exec(ls);
} 在Jsch中 ChannelExec负责通过SSH执行Shell命令。 首先在通道中设置命令然后必须启动通道。 它不像SSHJ那样直观。 // here: creation and configuration of session objectChannelExec execChannel null;
try {execChannel (ChannelExec) session.openChannel(exec);execChannel.connect();execChannel.setCommand(command);execChannel.start();
} catch (JSchException ex) {throw new IOException(ex);
} finally {if (execChannel ! null) {execChannel.disconnect();}
}远程主机上的文件操作 所有库都通过远程计算机上的SFTP支持或多或少的理想文件操作。 在SSHJ中 SFTPClient也具有用于文件操作的方法。 方法的名称与Linux系统上的文件操作相同。 以下代码段显示了如何删除文件。 //here: creation and configuration of sshClienttry (SFTPClient sftpClient sshClient.newSFTPClient()) {sftpClient.rm(remotePath);
} Commons VFS的核心功能是文件操作。 用法需要习惯。 必须解析文件对象并且可以对其执行文件操作。 // here: creation and configuration of remoteRootDirectoryFileObject remoteFileObject remoteRootDirectory.resolveFile(remotePath);
try {remoteFileObject.delete();
} finally {remoteFileObject.close();
} JSch的SFTPClient ChannelSFTP也具有文件操作方法。 此通道支持大多数文件操作。 例如必须通过ChannelExec上的简单外壳命令来完成远程计算机上的文件复制操作。 // here: creation and configuration of session
ChannelSftp sftpChannel null;
try {sftpChannel (ChannelSftp) session.openChannel(sftp);sftpChannel.connect();sftpChannel.rm(remotePath);
} catch (SftpException | JSchException ex) {throw new IOException(ex);
} finally {if (sftpChannel ! null) {sftpChannel.disconnect();}
}结论 比较之后我有两个收藏夹SSHJ和Commons VFS。 SSHJ具有非常清晰的API如果我需要通用的SSH客户端或通过SFTP进行文件操作就足够了我会选择它。 如果我可以通过许多文件系统协议进行文件操作或者不需要通用的SSH客户端则可以选择Commons VFS。 对于这两种情况我可以同时使用JSch来通过SSH执行命令。 Commons VFS的API已经习惯了。 但是在了解了背后的概念之后API的用法就很简单了。 这个比较的整个源代码示例都托管在Github上 。 有用的链接 SSHJ主页 JSch主页 Commons-vfs主页 有关SFTP的维基百科页面 SSHD主页 Github上此比较的源代码 翻译自: https://www.javacodegeeks.com/2015/08/commons-vfs-sshj-and-jsch-in-comparison.html