天天看点

JSch的使用与加载.ppk文件的注意事项JSch的使用与加载.ppk文件的注意事项

JSch的使用与加载.ppk文件的注意事项

JSch作为一个连接sftp的开源的jar包,使用时应该遵循下面几点注意事项

  • 如果私钥文件时putty生成的.ppk文件,那么为避免一系列麻烦事请遵循以下几点
    • jdk版本:1.8
    • JSch版本:0.1.55以上
             private String ftpServer;//登录服务器地址

             private String ftpUser;//登录用户

             private String ftpPassword;//登录用户密码

             private String ftpPrivateFilePath;//私钥证书文件路径

             private String ftpPassphrase;//私钥证书密码

             private int ftpPort;//登录服务器端口



                JSch jsch = new JSch();

                if (ftpPrivateFilePath != null) {

                     if (ftpPassphrase != null &&  !"".equals(ftpPassphrase)) {

                           jsch.addIdentity(ftpPrivateFilePath,  ftpPassphrase);// 设置私钥

                     } else {

                           jsch.addIdentity(ftpPrivateFilePath);// 设置私钥

                     }

                }

                sshSession = jsch.getSession(ftpUser, ftpServer,  ftpPort);

                if (ftpPassword != null &&  !"".equals(ftpPassword)) {

                     sshSession.setPassword(ftpPassword);

                }

                Properties sshConfig = new Properties();

                sshConfig.put("StrictHostKeyChecking", "no");

                sshSession.setConfig(sshConfig);

                sshSession.connect();

                log.info("SFTP:完成创建SFTP Session,准备创建SFTP  Channel");

                Channel channel =  sshSession.openChannel("sftp");

                channel.connect();

                sftp = (ChannelSftp) channel;

                log.info("SFTP:完成创建SFTP Channel");
           

继续阅读