Springboot 访问sftp方法(.ppk私钥转OpenSSH)

  1. 首选通过PuTTYgen要把.ppk私钥转换为OpenSSH, 否则JSch会报错
  • 加载.ppk文件, 输入私钥密码
    在这里插入图片描述
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/ec03fdd1678d4d8da202e80575d956d1.png在这里插入图片描述
  • 选择转换OpenSSH
    在这里插入图片描述
    在这里插入图片描述
  1. yaml配置
sftp:
  host: 111.111.111.111
  userName: test
  passWord: password
  port: 22
  prvkeyPath: D:/key/open_ssh # open_ssh是密钥文件,不是目录
  1. 链接sftp工具类
    关键代码:jsch.addIdentity(sftpProperties.getPrvkeyPath(), sftpProperties.getPassWord());
    /**
     * 连接sftp服务器
     */
    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异常");
        }
    }
  1. 测试
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中集成SFTP可以通过使用Apache Commons VFS(Virtual File System)库来实现。面是一个简单的示例代码: 1. 首先,确保在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-vfs2</artifactId> <version>2.7.0</version> </dependency> ``` 2. 创建一个SFTPUtil类,用于封装SFTP操作: ```java import org.apache.commons.vfs2.FileObject; import org.apache.commons.vfs2.FileSystemException; import org.apache.commons.vfs2.FileSystemOptions; import org.apache.commons.vfs2.Selectors; import org.apache.commons.vfs2.impl.StandardFileSystemManager; import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder; public class SFTPUtil { private static final String SFTP_URI = "sftp://username:password@hostname:port/path/to/directory"; public static void uploadFile(String localFilePath, String remoteFilePath) throws FileSystemException { StandardFileSystemManager manager = new StandardFileSystemManager(); try { manager.init(); FileObject localFile = manager.resolveFile(localFilePath); FileObject remoteFile = manager.resolveFile(SFTP_URI + remoteFilePath, createDefaultOptions()); remoteFile.copyFrom(localFile, Selectors.SELECT_SELF); } finally { manager.close(); } } private static FileSystemOptions createDefaultOptions() throws FileSystemException { FileSystemOptions options = new FileSystemOptions(); SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no"); SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, true); SftpFileSystemConfigBuilder.getInstance().setTimeout(options, 10000); // 设置连接超时时间 return options; } } ``` 3. 在你的代码中调用SFTPUtil类的uploadFile方法来上传文件: ```java public class Main { public static void main(String[] args) { try { SFTPUtil.uploadFile("path/to/local/file", "/path/to/remote/file"); System.out.println("File uploaded successfully!"); } catch (FileSystemException e) { e.printStackTrace(); } } } ``` 确保替换SFTP_URI中的`username`、`password`、`hostname`、`port`和`path/to/directory`为正确的SFTP连接信息。然后将本地文件路径和远程文件路径传递给uploadFile方法即可实现文件上传。 这只是一个简单的示例,你可以根据实际需求进行扩展和修改。另外,还可以使用其他的SFTP库,如JSch等,根据自己的喜好选择适合的库进行集成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值