Apache Mina SFTP 2.8.0 Upgrade 是一个用于 Java 应用程序实现 SFTP 协议的开源库。为了升级该库,可以按照以下步骤进行操作:
Maven:
org.apache.sshd
sshd-core
2.8.0
Gradle:
compile group: 'org.apache.sshd', name: 'sshd-core', version: '2.8.0'
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ChannelSftp;
import org.apache.sshd.client.config.hosts.HostConfigEntry;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
// 创建 SSH 客户端
SshClient client = SshClient.setUpDefaultClient();
client.start();
// 设置 SSH 连接配置
HostConfigEntry config = new HostConfigEntry();
config.setHostname("example.com");
config.setUsername("username");
config.setPort(22);
config.setIdentityFile(KeyUtils.detectKeyFile(new File("~/.ssh/id_rsa")));
// 创建 SSH 会话
ClientSession session = client.connect(config).verify().getSession();
// 鉴别 SSH 会话
session.addPasswordIdentity("password");
session.auth().verify();
// 创建 SFTP 频道
ChannelSftp channel = session.createSftpChannel();
// 关闭 SFTP 频道和 SSH 会话
channel.close(false);
session.close(false);
// 关闭 SSH 客户端
client.stop();
然后,使用以下代码建立 SFTP 连接:
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ChannelSftp;
import org.apache.sshd.client.config.host