【工具类】SFTP服务相关操作

1、SFTPChannel

import com.jcraft.jsch.*;
import org.jboss.logging.Logger;

import java.util.Map;
import java.util.Properties;

public class SFTPChannel {
    Session session = null;
    Channel channel = null;

    private static final Logger LOG = Logger.getLogger(SFTPChannel.class.getName());

    //登录sftp
    public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout) throws JSchException {
        String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
        String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
        String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
        String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);

        int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
        if (port != null && !port.equals("")) {
            ftpPort = Integer.valueOf(port);
        }

        //创建JSch对象
        JSch jsch = new JSch();
        //根据用户名,主机ip,端口获取一个Session对象
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
        LOG.debug("Session created.");
        if (ftpPassword != null) {
            session.setPassword(ftpPassword);
        }
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config); // 为Session对象设置properties
        session.setTimeout(timeout); // 设置timeout时间
        session.connect(); // 通过Session建立链接
        LOG.debug("Session connected.");
        LOG.debug("Opening Channel.");
        channel = session.openChannel("sftp"); // 打开SFTP通道
        channel.connect(); // 建立SFTP通道的连接
        LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
                + ", returning: " + channel);
        return (ChannelSftp) channel;
    }

    //退出sftp
    public void closeChannel() throws Exception {
        if (channel != null) {
            channel.disconnect();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}

2、SftpClientUtil

import com.jcraft.jsch.*;
import org.jboss.logging.Logger;
import java.io.*;
import java.util.*;

public class SftpClientUtil {
    private static Logger logger = Logger.getLogger(SftpClientUtil.class);
    private Session session;
    private Channel channel;
    private ChannelSftp sftp;
    private InputStream in;
    private OutputStream out;

    /**
     * 构造函数1
     *
     * @param host       主机
     * @param username   用户名
     * @param password   密码
     * @param port       端口
     * @param isHightSSH :jsch 与 ssh 版本适配,ssh>7.6 为true,反之为false
     * @throws Exception
     */
    public SftpClientUtil(String host, String username, String password, int port, boolean isHightSSH) throws Exception {
        JSch jsch = new JSch();
        this.session = jsch.getSession(username, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no"); // 不验证 HostKey
        if (isHightSSH) {
            config.put("kex", "diffie-hellman-group1-sha1,"
                    + "diffie-hellman-group-exchange-sha1,"
                    + "diffie-hellman-group-exchange-sha256"); //适配新版本ssh需添加对应的加密算法
        }
        session.setConfig(config);
        try {
            session.connect();
        } catch (Exception e) {
            if (session.isConnected())
                session.disconnect();
            logger.error("链接报错!!!", e);
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口[" + port + "],用户名[" + username + "],端口[" + port + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        channel = session.openChannel("sftp");
        try {
            if (channel.isConnected())
                channel.disconnect();
            channel.connect();
        } catch (Exception e) {
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口[" + port + "],用户名[" + username + "],端口[" + port + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        sftp = (ChannelSftp) channel;
    }

    /**
     * 构造函数2
     *
     * @param host     主机
     * @param username 用户名
     * @param password 密码
     * @param port     端口
     * @param encoding 字符集编码
     * @throws Exception
     */
    public SftpClientUtil(String host, String username, String password, int port, String encoding) throws Exception {
        JSch jsch = new JSch();
        this.session = jsch.getSession(username, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no"); // 不验证 HostKey
        session.setConfig(config);
        try {
            session.connect();
        } catch (Exception e) {
            if (session.isConnected())
                session.disconnect();
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口1[" + port + "],用户名[" + username + "],端口[" + port + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        channel = session.openChannel("sftp");
        try {
            channel.connect();
        } catch (Exception e) {
            if (channel.isConnected())
                channel.disconnect();
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口[" + port + "],用户名[" + username + "],密码[" + password + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        sftp = (ChannelSftp) channel;
        sftp.setFilenameEncoding(encoding);
    }

    /**
     * 构造函数3
     *
     * @param host     主机
     * @param username 用户名
     * @param password 密码
     * @param port     端口
     * @param encoding 字符集
     * @param timeout  超时时间
     * @throws Exception
     */
    private SftpClientUtil(String host, String username, String password, int port, String encoding, int timeout)
            throws Exception {
        JSch jsch = new JSch();
        this.session = jsch.getSession(username, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no"); // 不验证 HostKey
        session.setConfig(config);
        try {
            session.connect();
        } catch (Exception e) {
            if (session.isConnected())
                session.disconnect();
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口[" + port + "],用户名[" + username + "],密码[" + password + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        session.setTimeout(timeout);
        channel = session.openChannel("sftp");
        try {
            channel.connect();
        } catch (Exception e) {
            if (channel.isConnected())
                channel.disconnect();
            throw new Exception("连接服务器失败,请检查主机[" + host + "],端口[" + port + "],用户名[" + username + "],密码[" + password + "]是否正确,以上信息正确的情况下请检查网络连接是否正常或者请求被防火墙拒绝.");
        }
        sftp = (ChannelSftp) channel;
        sftp.setFilenameEncoding(encoding);
    }

    /**
     * 文件上传
     *
     * @param remotePath     远端路径
     * @param remoteFileName 上传到远端后的文件名
     * @param localPath      本地路径
     * @param localFileName  本地文件名
     * @return
     */
    public boolean uploadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
        FileInputStream in = null;
        try {
            createDir(remotePath);
            File file = new File(localPath + localFileName);
            in = new FileInputStream(file);
            sftp.put(in, remoteFileName);
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    //创建目录
    public boolean createDir(String createpath) {
        try {
            createpath = createpath.trim();
            if (isDirExist(createpath)) {
                this.sftp.cd(createpath);
                logger.info("cd " + createpath);
                return true;
            }
            String[] pathArray = createpath.split("/");
            for (String path : pathArray) {
                path = path.trim();
                if ("".equals(path)) {
                    continue;
                }
                if (!isDirExist(path)) {
                    sftp.mkdir(path);
                }
                logger.info("cd " + path);
                sftp.cd(path);
            }
            return true;
        } catch (SftpException e) {
            e.printStackTrace();
        }
        return false;
    }

    //判断目录是否存在
    public boolean isDirExist(String directory) {
        boolean isDirExistFlag = false;
        try {
            SftpATTRS sftpATTRS = sftp.lstat(directory);
            return sftpATTRS.isDir();
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                logger.error("directory:" + directory + ",no such file ERROR!!!");
            }
        }
        return isDirExistFlag;
    }

    /**
     * 文件下载
     *
     * @param remotePath    远程文件存放路径
     * @param localPath     下载到本地的路径
     * @return 下载后的文件名集合
     * @throws Exception
     */
    public List<String> downloadFiles(String remotePath, String localPath){
        List<String> downloadFiles = new ArrayList<String>();

        try {
            logger.info("切换到指定目录:" + remotePath);
            boolean flag = openDir(remotePath, sftp);
            if (flag) {
                //1.获取远端路径下所有文件
                Vector<?> vv = listFiles("*");
                if (vv == null) {
                    return null;
                } else {
                    for (Object object : vv) {
                        ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) object;
                        String remoteFileName = entry.getFilename();

                        logger.info("校验文件名:" + remoteFileName);
                        String path = localPath.substring(localPath.length() - 1).equals("/") ? localPath : localPath + "/";
                        File file = new File(path + remoteFileName);
                        logger.info("保存校对文件的本地路径为:" + file.getAbsolutePath());

                        logger.info("start downLoad " + remoteFileName + " ~~");
                        sftp.get(remoteFileName, new FileOutputStream(file));
                        logger.info("downLoad ok ~~");

                        downloadFiles.add(remoteFileName);
                    }

                    if (downloadFiles.size() < 1) {
                        logger.error("remotePath:" + remotePath + "路径下,未匹配到校对文件!");
                    }
                }
            } else {
                logger.info("对应的目录" + remotePath + "不存在!");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sftp != null) {
                if (sftp.isConnected()) {
                    sftp.disconnect();
                }
            }
            if (session != null) {
                if (session.isConnected()) {
                    session.disconnect();
                }
            }
        }
        return downloadFiles;
    }

    //打开或者进入指定目录
    public boolean openDir(String directory, ChannelSftp sftp) {
        try {
            sftp.cd(directory);
            logger.info("cd " + directory + " ok");
            return true;
        } catch (SftpException e) {
            logger.error(e + "");
            return false;
        }
    }

    //返回目录下所有文件信息
    public Vector listFiles(String directory) throws SftpException {
        return sftp.ls(directory);
    }

    /**
     * 断开与主机的连接
     */
    public void disconnect() {
        try {
            sftp.disconnect();
        } catch (Exception ignored) {
        }
        try {
            channel.disconnect();
        } catch (Exception ignored) {
        }
        try {
            session.disconnect();
        } catch (Exception ignored) {
        }
    }

    /**
     * 得到SFTP实例
     *
     * @param host     主机
     * @param username 用户名
     * @param password 密码
     * @param port     端口
     * @return
     * @throws Exception
     */
    public static SftpClientUtil getInstans(String host, String username, String password, int port) throws Exception {
        return new SftpClientUtil(host, username, password, port, false);
    }

    /**
     * 得到SFTP实例
     *
     * @param host     主机
     * @param username 用户名
     * @param password 密码
     * @param port     端口
     * @param encoding 字符集编码
     * @return
     * @throws Exception
     */
    public static SftpClientUtil getInstans(String host, String username, String password, int port, String encoding)
            throws Exception {
        return new SftpClientUtil(host, username, password, port, encoding);
    }

    /**
     * 得到SFTP实例
     *
     * @param host     主机
     * @param username 用户名
     * @param password 密码
     * @param port     端口
     * @param encoding 字符集编码
     * @param timeout  超时时间
     * @return
     * @throws Exception
     */
    public static SftpClientUtil getInstans(String host, String username, String password, int port, String encoding,
                                            int timeout) throws Exception {
        return new SftpClientUtil(host, username, password, port, encoding, timeout);
    }

3、SFTPConstants

public class SFTPConstants {
    public static final String SFTP_REQ_HOST = "192.168.1.1";
    public static final String SFTP_REQ_PORT = "22";
    public static final String SFTP_REQ_USERNAME = "mulu";
    public static final String SFTP_REQ_PASSWORD = "mima";
    public static final int SFTP_DEFAULT_PORT = 22;
    public static final String SFTP_REQ_LOC = "location";

4、测试

SftpClientUtil sftp = new SftpClientUtil(SFTPConstants.SFTP_REQ_HOST,SFTPConstants.SFTP_REQ_USERNAME,SFTPConstants.SFTP_REQ_PASSWORD, SFTPConstants.SFTP_DEFAULT_PORT,"UTF-8");
        List<String> files = sftp.downloadFiles("/mulu","/bendimulu");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

司徒剑南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值