mitao
2025-05-20 179c4d64313c9b7572778da4aaaf6c6584fe457d
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java
@@ -1,14 +1,15 @@
package com.panzhihua.common.utlis;
import com.jcraft.jsch.*;
import com.panzhihua.common.constants.FtpConstants;
import java.io.*;
import java.util.Properties;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Properties;
import java.util.Vector;
import com.jcraft.jsch.*;
import com.panzhihua.common.constants.FtpConstants;
/**
 * @version 1.0.0
@@ -52,6 +53,16 @@
        this.host = FtpConstants.FTPHOST;
        this.port = FtpConstants.FTPPORT;
    }
    public static void main(String[] args) throws SftpException, IOException {
//        SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22);
        SFTPUtil sftp = new SFTPUtil("root", "Wpkl#&%!698", "172.192.3.2", 22);
        sftp.login();
        File file = new File("F:\\settings.xml");
        InputStream is = new FileInputStream(file);
        // 多级目录创建并上传
        sftp.uploadMore("/mnt/data/ftp/ss", "settings.xml", is);
        sftp.logout();
    }
    /**
     * 连接sftp服务器
@@ -79,10 +90,11 @@
            channel.connect();
            log.info("channel is connected");
            sftp = (ChannelSftp) channel;
            sftp = (ChannelSftp)channel;
            log.info(String.format("sftp server host:[%s] port:[%s] is connect successfull", host, port));
        } catch (JSchException e) {
            log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", new Object[]{host, port, e.getMessage()});
            log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", host, port,
                e.getMessage());
        }
    }
@@ -107,10 +119,14 @@
    /**
     * 递归根据路径创建文件夹
     *
     * @param dirs     根据 / 分隔后的数组文件夹名称
     * @param tempPath 拼接路径
     * @param length   文件夹的格式
     * @param index    数组下标
     * @param dirs
     *            根据 / 分隔后的数组文件夹名称
     * @param tempPath
     *            拼接路径
     * @param length
     *            文件夹的格式
     * @param index
     *            数组下标
     * @return
     */
    public void mkdirDir(String[] dirs, String tempPath, int length, int index) {
@@ -144,9 +160,12 @@
    /**
     * 将输入流的数据上传到sftp作为文件(多层目录)
     *
     * @param directory    上传到该目录(多层目录)
     * @param sftpFileName sftp端文件名
     * @param input        输入流
     * @param directory
     *            上传到该目录(多层目录)
     * @param sftpFileName
     *            sftp端文件名
     * @param input
     *            输入流
     * @throws SftpException
     * @throws Exception
     */
@@ -166,9 +185,12 @@
    /**
     * 将输入流的数据上传到sftp作为文件
     *
     * @param directory    上传到该目录(单层目录)
     * @param sftpFileName sftp端文件名
     * @param input        输入流
     * @param directory
     *            上传到该目录(单层目录)
     * @param sftpFileName
     *            sftp端文件名
     * @param input
     *            输入流
     * @throws SftpException
     * @throws Exception
     */
@@ -187,8 +209,10 @@
    /**
     * 上传单个文件
     *
     * @param directory  上传到sftp目录
     * @param uploadFile 要上传的文件,包括路径
     * @param directory
     *            上传到sftp目录
     * @param uploadFile
     *            要上传的文件,包括路径
     * @throws FileNotFoundException
     * @throws SftpException
     * @throws Exception
@@ -201,9 +225,12 @@
    /**
     * 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。
     *
     * @param directory    上传到sftp目录
     * @param sftpFileName 文件在sftp端的命名
     * @param byteArr      要上传的字节数组
     * @param directory
     *            上传到sftp目录
     * @param sftpFileName
     *            文件在sftp端的命名
     * @param byteArr
     *            要上传的字节数组
     * @throws SftpException
     * @throws Exception
     */
@@ -214,29 +241,38 @@
    /**
     * 将字符串按照指定的字符编码上传到sftp
     *
     * @param directory    上传到sftp目录
     * @param sftpFileName 文件在sftp端的命名
     * @param dataStr      待上传的数据
     * @param charsetName  sftp上的文件,按该字符编码保存
     * @param directory
     *            上传到sftp目录
     * @param sftpFileName
     *            文件在sftp端的命名
     * @param dataStr
     *            待上传的数据
     * @param charsetName
     *            sftp上的文件,按该字符编码保存
     * @throws UnsupportedEncodingException
     * @throws SftpException
     * @throws Exception
     */
    public void upload(String directory, String sftpFileName, String dataStr, String charsetName) throws UnsupportedEncodingException, SftpException {
    public void upload(String directory, String sftpFileName, String dataStr, String charsetName)
        throws UnsupportedEncodingException, SftpException {
        upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName)));
    }
    /**
     * 下载文件
     *
     * @param directory    下载目录
     * @param downloadFile 下载的文件
     * @param saveFile     存在本地的路径
     * @param directory
     *            下载目录
     * @param downloadFile
     *            下载的文件
     * @param saveFile
     *            存在本地的路径
     * @throws SftpException
     * @throws FileNotFoundException
     * @throws Exception
     */
    public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException {
    public void download(String directory, String downloadFile, String saveFile)
        throws SftpException, FileNotFoundException {
        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
@@ -248,8 +284,10 @@
    /**
     * 下载文件
     *
     * @param directory    下载目录
     * @param downloadFile 下载的文件名
     * @param directory
     *            下载目录
     * @param downloadFile
     *            下载的文件名
     * @return 字节数组
     * @throws SftpException
     * @throws IOException
@@ -268,8 +306,10 @@
    /**
     * 下载文件
     *
     * @param directory    下载目录
     * @param downloadFile 下载的文件名
     * @param directory
     *            下载目录
     * @param downloadFile
     *            下载的文件名
     * @return 字节数组
     * @throws SftpException
     * @throws IOException
@@ -280,16 +320,18 @@
            sftp.cd(directory);
        }
        InputStream is = sftp.get(downloadFile);
//        byte[] fileData = IOUtils.toByteArray(is);
//        log.info("file:{} is download successful", downloadFile);
        // byte[] fileData = IOUtils.toByteArray(is);
        // log.info("file:{} is download successful", downloadFile);
        return is;
    }
    /**
     * 删除文件
     *
     * @param directory  要删除文件所在目录
     * @param deleteFile 要删除的文件
     * @param directory
     *            要删除文件所在目录
     * @param deleteFile
     *            要删除的文件
     * @throws SftpException
     * @throws Exception
     */
@@ -301,7 +343,8 @@
    /**
     * 列出目录下的文件
     *
     * @param directory 要列出的目录
     * @param directory
     *            要列出的目录
     * @return
     * @throws SftpException
     */
@@ -311,11 +354,13 @@
    /**
     * 判断文件或者目录是否存在
     * @param path 目录或者文件的路径
     *
     * @param path
     *            目录或者文件的路径
     * @return 存在结果
     */
    public boolean isExistDir(String path){
        boolean  isExist=false;
    public boolean isExistDir(String path) {
        boolean isExist = false;
        try {
            SftpATTRS sftpATTRS = sftp.lstat(path);
            isExist = true;
@@ -327,16 +372,5 @@
        }
        return isExist;
    }
    public static void main(String[] args) throws SftpException, IOException {
        SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22);
        sftp.login();
        File file = new File("F:\\settings.xml");
        InputStream is = new FileInputStream(file);
        // 多级目录创建并上传
        sftp.uploadMore("/mnt/data/ftp/ss", "settings.xml", is);
        sftp.logout();
    }
}