java sftp 客户端_java SFTP实现连接linux客户端上传与下载

本文介绍了如何使用Java的JSch库实现SFTP连接,以连接到Linux客户端进行文件的上传和下载操作。通过提供详细的代码示例,展示了如何设置连接参数、建立连接、读写文件以及断开连接等步骤。
摘要由CSDN通过智能技术生成

java SFTP实现连接linux客户端上传与下载

package com.neusoft.mid5.framework.common.sftp;

import com.google.common.base.Strings;

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;

import com.jcraft.jsch.SftpATTRS;

import com.jcraft.jsch.SftpException;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Properties;

import java.util.Vector;

/**

* @Description: SFTP客户端

* @author zhang.xinjie@neusoft.com

* @date 2020年9月10日 下午14:00:03

*/

public class SftpClient {

private final static Logger logger = LoggerFactory.getLogger(SftpClient.class);

private static String host = "客户端ip";

private static String username = "用户名";

private static String password = "密码";

protected static String privateKey;// 密钥文件路径

protected static String passphrase;// 密钥口令

private static int port = 22;

private static ChannelSftp sftp = null;

private static Session sshSession = null;

public SftpClient(String host, String username, String password) {

this.host = host;

this.username = username;

this.password = password;

}

public SftpClient(String host, String username, String password, int port) {

this.host = host;

this.username = username;

this.password = password;

this.port = port;

}

public SftpClient(String host, String username, String password, int port, String privateKey, String passphrase) {

this.host = host;

this.username = username;

this.password = password;

this.privateKey = privateKey;

this.passphrase = passphrase;

this.port = port;

}

public static void connect() {

JSch jsch = new JSch();

Channel channel = null;

try {

if (!StringUtils.isEmpty(privateKey)) {

// 使用密钥验证方式,密钥可以使有口令的密钥,也可以是没有口令的密钥

if (!StringUtils.isEmpty(passphrase)) {

jsch.addIdentity(privateKey, passphrase);

} else {

jsch.addIdentity(privateKey);

}

}

sshSession = jsch.getSession(username, host, port);

if (!StringUtils.isEmpty(password)) {

sshSession.setPassword(password);

}

Properties sshConfig = new Properties();

sshConfig.put("StrictHostKeyChecking", "no");// do not verify host

// key

sshSession.setConfig(sshConfig);

// session.setTimeout(timeout);

// session.setServerAliveInterval(92000);

sshSession.connect();

// 参数sftp指明要打开的连接是sftp连接

channel = sshSession.openChannel("sftp");

channel.connect();

sftp = (ChannelSftp) channel;

} catch (JSchException e) {

logger.error("连接【" + host + &#

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值