- 首选通过PuTTYgen要把.ppk私钥转换为OpenSSH, 否则JSch会报错
- 加载.ppk文件, 输入私钥密码
![在这里插入图片描述](https://img-blog.csdnimg.cn/ec03fdd1678d4d8da202e80575d956d1.png - 选择转换OpenSSH
- yaml配置
sftp:
host: 111.111.111.111
userName: test
passWord: password
port: 22
prvkeyPath: D:/key/open_ssh
- 链接sftp工具类
关键代码:jsch.addIdentity(sftpProperties.getPrvkeyPath(), sftpProperties.getPassWord());
private void connect() {
log.info("ftp连接开始host=" + sftpProperties.getHost() + "port" + sftpProperties.getPort() + "username=" + sftpProperties.getUserName());
JSch jsch = new JSch();
try {
jsch.addIdentity(sftpProperties.getPrvkeyPath(), sftpProperties.getPassWord());
jsch.getSession(sftpProperties.getUserName(), sftpProperties.getHost(), sftpProperties.getPort());
sshSession = jsch.getSession(sftpProperties.getUserName(), sftpProperties.getHost(), sftpProperties.getPort());
log.info("ftp---Session created.");
sshSession.setPassword(sftpProperties.getPassWord());
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
sshSession.setConfig(properties);
sshSession.connect();
log.info("ftp---Session connected.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
log.info("Opening Channel.");
sftp = (ChannelSftp) channel;
log.info("ftp---Connected to " + sftpProperties.getHost());
} catch (JSchException e) {
log.error("sftp connect异常", e);
throw new FileSyncException("connect异常");
}
}
- 测试
…