package com.xydl.cac.util;

import com.jcraft.jsch.*;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.Properties;
import java.util.Vector;

public class SFTPTool {
    Session session = null;
    ChannelSftp sftp = null;

    /**
     * 1. 第一步:连接sftp服务器
     *
     * @param host     主机
     * @param port     端口
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public void connect(String host, int port, String username, String password) throws JSchException {
        JSch jsch = new JSch();
        session = jsch.getSession(username, host, port);
        session.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        session.setConfig(sshConfig);
        session.connect();
        Channel channel = session.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;
    }

    /**
     * 3.最后一步:关闭 channel和session
     */
    public void disconnect() {
        try {
            if (sftp != null && sftp.isConnected()) {
                sftp.disconnect();
            }
        } catch (Exception ignore) {
        }
        try {
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        } catch (Exception ignore) {
        }
    }

    public void upload(String localFilePath, String remoteFileName) throws Exception {
        sftp.put(new FileInputStream(localFilePath), remoteFileName);
    }

    public void download(String remoteFileName, String localFilePath) throws Exception {
        String tmpFile = localFilePath + ".tmp";
        sftp.get(remoteFileName, new FileOutputStream(tmpFile));
        File file = new File(tmpFile);
        file.renameTo(new File(localFilePath));
    }

    public void delete(String remoteFileName) throws SftpException {
        sftp.rm(remoteFileName);
    }

    public Vector listFiles(String remotePath) throws SftpException {
        sftp.cd(remotePath);
        return sftp.ls(remotePath);
    }

    public void cdmkdir(String remotePath) throws SftpException {
        if (StringUtils.isNotBlank(remotePath)) {
            if (remotePath.startsWith("/")) {
                sftp.cd("/");
            }
            String[] dirs = remotePath.split("/");
            for (String dir : dirs) {
                if (StringUtils.isNotBlank(dir)) {
                    try {
                        sftp.cd(dir);
                    } catch (SftpException e) {
                        sftp.mkdir(dir);
                        sftp.cd(dir);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        SFTPTool sftpTool = new SFTPTool();
        try {
            String path = "abc/aaa";
            sftpTool.connect("192.168.1.190", 22, "root", "123456");
            sftpTool.cdmkdir(path);
            sftpTool.upload("C:\\Code\\cac\\局放谱图\\50100055_5__001_01_20250409175800.dat",
                    "50100055_5__001_01_20250409175800.dat");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sftpTool.disconnect();
        }
    }

}
