package com.tzy.framework.pay.util.sftp;

import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;

@Slf4j
public class SFtpTools {

    private  String ip ;     //IP地址
    private  Integer port;    //端口
    private  String username;//用户名
    private  String password;//密码
    private  String uploadRemotePath;    //上传到服务器的路径
    private  String downloadRemotePath;    //从服务器指定路径下载

    public SFtpTools() {
        super();
    }
    /**
     *
     * @param ip
     * @param port
     * @param username
     * @param password
     * @param uploadRemotePath 上传到服务器的路径
     * @param downloadRemotePath 从服务器指定路径下载
     */
    public SFtpTools(String ip, Integer port, String username,String password, String uploadRemotePath,String downloadRemotePath) {
        super();
        this.ip = ip;
        this.port = port;
        this.username = username;
        this.password = password;
        this.uploadRemotePath = uploadRemotePath;
        this.downloadRemotePath = downloadRemotePath;
    }

    public void uploadFiles(File uploadFile) throws JSchException, IOException, SftpException {
        log.info("connect to SFTP . ip:" + this.ip + "; port:" + this.port + ";uppath:" + this.uploadRemotePath + ";downpath:" + this.downloadRemotePath+ ";username:" + this.username + ";password:" + this.password);
        SFtpClient sFtpClient = new SFtpClient(ip, port, uploadRemotePath, downloadRemotePath,username, password);
        sFtpClient.connect();
        // 上传文件
        log.info("sftp sending file....local file path = "+uploadFile.getPath());
        sFtpClient.uploadFile(uploadFile);
        // 单台链接关闭
        sFtpClient.disconnect();
    }

    /**
     * 下载文件到指定的路径
     * @param downloadFile
     * @param remeotFileName 要从服务器获取的文件名称
     * @throws JSchException
     * @throws IOException
     * @throws SftpException
     */
    public void downloadFiles(String remeotFileName, File downloadFile) throws JSchException, IOException, SftpException {
        log.info("connect to SFTP . ip:" + this.ip + "; port:" + this.port + ";uppath:" + this.uploadRemotePath + ";downpath:" + this.downloadRemotePath+ ";username:" + this.username + ";password:" + this.password);
        SFtpClient sFtpClient = new SFtpClient(ip, port, uploadRemotePath, downloadRemotePath,username, password);
        sFtpClient.connect();
        // 下载文件
        log.info("sftp receiving file....remote file name = "+remeotFileName);
        sFtpClient.downloadFile(remeotFileName,downloadFile);
        // 单台链接关闭
        sFtpClient.disconnect();
    }

    public static void main(String[] args) throws IOException {
//        String ip = "180.169.95.129";
        String ip = "140.206.74.65";
        SFtpTools TPMX = new SFtpTools("180.169.95.129", 22, "shpydzsw_sftp", "4Wa!m7Bf","", "/upload/TPMX/20220427");
        SFtpTools JYMX = new SFtpTools(ip, 22, "SHPYDZ_fan", "aL1Jn$kg","", "/upload/JYMX/");
        File file = new File("D:\\home\\tzy\\uploadpath\\qm\\SHPYDZ_settleJournal_20220428.txt");
        try {
            if(file.exists()){
                file.mkdirs();
            }
            JYMX.downloadFiles("SHPYDZ_settleJournal_20220428.txt",file);
        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }finally {

        }


    }

}
