SFTP链接远程服务器

import java.util.Properties;

import com.cloudibpm.core.util.SystemConfig;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class SFTPUtils {

    protected static String host = SystemConfig.getProp("hostIp");
    protected static String username = SystemConfig.getProp("username");
    protected static String password = SystemConfig.getProp("password");
    protected static String str = SystemConfig.getProp("port");
    protected static String privateKey = SystemConfig.getProp("privateKey");
    protected static String passphrase = SystemConfig.getProp("passphrase");
    
    static Session session = null;
    static Channel channel = null;
    
     public static ChannelSftp connectSFTP() throws JSchException {
         int port = Integer.parseInt(str);
         JSch jsch = new JSch();
          if (privateKey != null && !"".equals(privateKey)) {
              //使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥
              if (passphrase != null && "".equals(passphrase)) {
                  jsch.addIdentity(privateKey, passphrase);
              } else {
                  jsch.addIdentity(privateKey);
              }
          }
         Session session = jsch.getSession(username, host, port);
         if(password!=null){
             session.setPassword(password);//设置密码
         }
         Properties conf = new Properties();
         conf.put("StrictHostKeyChecking","no");
         session.setConfig(conf);//为session对象设置properties
         session.setTimeout(1000);
         session.connect();
         
         channel = session.openChannel("sftp");
         channel.connect();
         return (ChannelSftp) channel;
     }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值