java ftp秘钥文件登陆_java sftp传输文件密码密钥方式

packagexxx.utilsimport com.jcraft.jsch.*

importorg.slf4j.Loggerimportorg.slf4j.LoggerFactoryimportjava.text.SimpleDateFormat

//使用的是groovyclassSftpUtil {private static final Logger logger =LoggerFactory.getLogger(SftpUtil.getClass())static SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd")

JSch jsch= nullSession session= nullChannelSftp channel= null

static ThreadLocal sftpLocal = new ThreadLocal()

SftpUtil(String userName, String host,intport, String password) {

connect(userName, host, port, password)

}

SftpUtil(String userName, String host,intport, String password, String keyFilePath, String passphrase) {

connect(userName, host, port, password, keyFilePath, passphrase)

}booleanisConnected() {return null != channel &&channel.isConnected()

}static SftpUtil getSftpUtil(String userName, String host, int port, String password) throwsException {

SftpUtil sftpUtils=sftpLocal.get()if (sftpUtils == null || !sftpUtils.isConnected()) {

sftpLocal.set(newSftpUtil(userName, host, port, password))

}returnsftpLocal.get()

}static SftpUtil getSftpUtil(String userName, String host, int port, String password, String keyFilePath, String passphrase) throwsException {

SftpUtil sftpUtils=sftpLocal.get()if (sftpUtils == null || !sftpUtils.isConnected()) {

sftpLocal.set(newSftpUtil(userName, host, port, password, keyFilePath, passphrase))

}returnsftpLocal.get()

}static voidrelease() {if (null !=sftpLocal.get()) {

sftpLocal.get().close()

sftpLocal.set(null)

}

}/*** 连接到指定的IP

*

*@throwsJSchException*/ChannelSftp connect(String userName, String host,intport, String password) {try{

jsch= new JSch()//创建JSch对象

session = jsch.getSession(userName, host, port)//根据用户名、主机ip、端口号获取一个Session对象

session.setConfig("PreferredAuthentications", "password")

session.setPassword(password)

session.setConfig("StrictHostKeyChecking", "no")

session.setTimeout(60000)

session.setServerAliveInterval(2000)

session.setServerAliveCountMax(8)

session.connect(3000)

logger.info("sftp session connected.")

channel= (ChannelSftp) session.openChannel("sftp")

channel.connect(3000)

logger.info("Connected successfully ${host} ${userName}")

}catch(

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值