使用Java连接SFTP服务器(在服务器上创建,获取,上传文件)

Java连接SFTP服务器步骤

调用测试(这里是在service层中进行测试),相关的连接配置信息可在application.yml文件里面配置。
在这里插入图片描述
application.yml中的配置信息:
在这里插入图片描述

使用@Value注解去进行单个从yml文件中获取
在这里插入图片描述

1.创建一个专门用于操作ftp的工具类,SFTPUtil,方便于后期的管理以及复用工具类(在最后面有整个工具类完整的代码
在这里插入图片描述
2.工具类中的属性如下。

//用于打印日志文件  以查看日志文件结果
private final static Logger logger = LoggerFactory.getLogger(FileService.class);
//连接ftp服务器响应时间  以下通常为毫秒
private final static int CLIENT_TIMEOUT = 3600* 1000;
//引入的maven依赖包中的类
ChannelSftp channelSftp = null;     //所有操作公用一个Channel
//ftp服务器中的IP地址
private String serverIP;
// 端口号
private Integer	port ;
 //用户名
private String username;
 //密码
private String password;
 //编码格式
private String encoding = "UTF-8";

构造方法

//构造方法
public SFTPUtils(String serverIP, Integer port, String username, String password){
        this.serverIP = serverIP;
        if(port!=null){
            this.port = port;
        }
        this.username = username;
        this.password = password;
    }

//返回单一对象的方法
public static SFTPUtils newInstance(String serverIP, Integer port, String username, String password) {
        return new SFTPUtils(serverIP,port,username,password);
    }

在这里插入图片描述
在这里插入图片描述

3.在工具类中写一个用于连接,验证ftp服务器的方法connectFtp(),方法中的内容如下。

public void connectFtp() throws Exception{
        logger.info("SftpUtils start beginning");
        Session session = null;
        try {
            JSch jSch = new JSch();
            session = jSch.getSession(username,serverIP,port);
            session.setTimeout(CLIENT_TIMEOUT);
            if(! StringUtils.isEmpty(password)){
                session.setPassword(password);
            }
            Properties properties = new Properties();
            properties.setProperty("StrictHostKeyChecking","no");
            session.setConfig(properties);
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();

            channelSftp = (ChannelSftp)channel;

        }catch (Exception e){
        //需要写一个枚举类 用于显示错误信息
            logger.info(FtpErrorCode.CONNECT_SERVER_FAILER.getErrorDesc());
            throw e;
        }
    }

4.写一个disConnect()方法,用于判断是否已成功连接到sftp服务器,若未成功连接,则返回错误日志信息,其方法中的内容如下:(FtpErrorCode为异常枚举类)

public void disConnect() throws Exception{
        logger.info("SftpUtils end beginning...");
        if(channelSftp != null){
            Session session= null;
            try {
                session = channelSftp.getSession();
            }catch (JSchException e){
                //需要写一个枚举类 用于显示错误日志信息
                logger.info(FtpErrorCode.DISCONNECT_SERVER_GET_CONNECT_FAILER.getErrorDesc());
                throw e;
            }finally {
                if(session !=null){
                    session.disconnect();
                }
            }
        }
    }

5.在远程ftp服务器上创建文件夹,mkdirs(),多级创建文件

/**
 * 如果远程目录不存在,则创建远程目录
 * @param remoteDirectory               /abc/sdf/sdf/adf   多级目录
 */
void mkdirs(String remoteDirectory) throws Exception{
    try {
        channelSftp.cd(remoteDirectory);
    } catch (SftpException e) {
        String[] dirs = remoteDirectory.replaceAll("\\\\","/").split("/"); //{abc,sdf,sdf,adf}
        String tempPath = "";
        for(String dir : dirs){
            if(StringUtils.isEmpty(dir)){
                continue;
            }

            //此处不能使用File.separator
            tempPath += "/" + dir;
            try {
                channelSftp.cd(tempPath);
            } catch (SftpException sftpException) {
                try {
                    channelSftp.mkdir(tempPath);
                    channelSftp.cd(tempPath);
                } catch (Exception exception) {
                    this.disConnect();
                    logger.info(FtpErrorCode.CREATE_DIRECTORY_FAILER.getErrorDesc());
                    throw exception;
                }
            }
        }
    }
    try {
        channelSftp.cd(remoteDirectory.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        this.disConnect();
        logger.info(FtpErrorCode.CONNECT_SERVER_FAILER.getErrorDesc());
        throw e;
    }
}

6.本地文件上传到ftp服务器中,写一个uploadFile()方法,方法中的内容如下:

形式1:

/**
 * 将本地文件上传到远程ftp服务器某目录中
 * @param localFile           本地文件全路径
 * @param remoteFile          远程文件全路径
 */
public void uploadFile(String localFile, String remoteFile) throws Exception{
    logger.info("SftpUtils upload beginning");
    InputStream inputStream =null;
    logger.info("1. 连接ftp服务器");
    this.connectFtp();
    logger.info("2. 转换本地文件为输入流");
    try {
        inputStream = new FileInputStream(localFile);
    } catch (FileNotFoundException e) {
        this.disConnect();
        logger.info(FtpErrorCode.GET_LOCAL_FILE_INPUT_STREAM.getErrorDesc());
        throw e;
    }
    logger.info("3. 创建远程目录文件夹");
    String fileNameAbsolute = remoteFile.replaceAll("\\\\","/");
    String remoteDirectory = fileNameAbsolute.substring(0,fileNameAbsolute.lastIndexOf("/"));

    logger.info("remoteDirectory"+remoteDirectory);

    mkdirs(remoteDirectory);

    logger.info("4. 保存输入流到sftp服务器");
    try {
        channelSftp.put(inputStream,remoteFile.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        logger.info(FtpErrorCode.UPLOAD_FILE_FAILER.getErrorDesc());
        throw e;
    }finally {
        this.disConnect();
    }
}

形式2:

/**
 * 将内存数据转换为输入流上传
 * @param localInputStream           本地输入流
 * @param remoteFile          远程文件全路径
 */
public void uploadFile(InputStream localInputStream, String remoteFile) throws Exception{
    logger.info("SftpUtils upload beginning");
    logger.info("1. 连接ftp服务器");
    this.connectFtp();

    logger.info("2. 创建远程目录文件夹");
    String fileNameAbsolute = remoteFile.replaceAll("\\\\","/");
    String remoteDirectory = fileNameAbsolute.substring(0,fileNameAbsolute.lastIndexOf("/"));
    mkdirs(remoteDirectory);

    logger.info("4. 保存输入流到sftp服务器");
    try {
        channelSftp.put(localInputStream,remoteFile.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        logger.info(FtpErrorCode.UPLOAD_FILE_FAILER.getErrorDesc());
        throw e;
    }finally {
        this.disConnect();
    }
}

7.从ftp远程服务器,某个目录下下载文件到本地服务器中,写一个方法ftpDownLoad();

形式1:(下载到本地)

/**
 * @param remoteFile   远程文件全路径
 * @param localFile    本地系统目录全路径
 */
public void ftpDownLoad(String remoteFile,String localFile) throws Exception{
    logger.info("SftpUtils download beginning");
    logger.info("1. 连接ftp服务器");
    this.connectFtp();
    logger.info("2. 创建本地文件输入流");
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(localFile);
    } catch (FileNotFoundException e) {
        this.disConnect();
        logger.info(FtpErrorCode.GET_LOCAL_FILE_OUTPUT_STREAM.getErrorDesc());
        throw new RuntimeException(e);
    }
    logger.info("3. 下载到本地文件/输出流");
    try {
        channelSftp.get(remoteFile.replaceAll("\\\\","/"),outputStream);
    } catch (SftpException e) {
        this.disConnect();
        logger.info(FtpErrorCode.GET_LOCAL_FILE_OUTPUT_STREAM.getErrorDesc());
        throw e;
    }finally {
        logger.info("4. 关闭sftp的连接");
        this.disConnect();
    }
}

形式2:读取远程文件中的内容,并返回inputStream流对象(不下载至本地)

 //读取远程文件中的内容,并返回流  remoteFile:远程文件全路径 如:download/sds.csv
public InputStream sftpWriter(String remoteFile){

    InputStream is=null;
    try {
        logger.info("SftpUtils download beginning");
        logger.info("1. 连接ftp服务器");
        this.connectFtp();
        logger.info("2. 创建远程文件输出流");
        is = channelSftp.get(remoteFile);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (SftpException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            logger.info("3. 关闭sftp的连接");
            this.disConnect();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    return is;

}

8.删除ftp服务器某个目录下的文件,写一个ftpDelete()方法。

public void ftpDelete(String remoteFile) throws Exception{
    logger.info("SftpUtils download beginning");
    logger.info("1. 连接ftp服务器");
    this.connectFtp();
    logger.info("2. 删除远端文件");
    String directory = remoteFile.replaceAll("\\\\","/").substring(0,remoteFile.lastIndexOf("/"));
    try {
        channelSftp.cd(directory);
    } catch (SftpException e) {
        this.disConnect();
        logger.info(FtpErrorCode.DELETE_FTP_FILE_FAILER.getErrorDesc());
        throw e;
    }
//        String fileName = remoteFile.substring(remoteFile.replaceAll("\\\\","/").lastIndexOf("/") + 1);
        try {
            channelSftp.rm(remoteFile);//remove()
        } catch (SftpException e) {
            this.disConnect();
            logger.info(FtpErrorCode.DELETE_FTP_FILE_FAILER.getErrorDesc());
            throw e;
        }finally {
            logger.info("4. 关闭sftp的连接");
            this.disConnect();
        }
    }

9.查找目录下的所有文件,写一个方法findFiles(),返回文件list集合。

形式1

/**
 * 查找目录下的所有文件
 * @param remoteFileDirectory
 * @return
 */
public List<String> findFiles(String remoteFileDirectory) throws Exception{
    logger.info("find directory files beginning...");
    List<String> listFile = new ArrayList<String>();
    logger.info("1. 连接ftp服务器");
    this.connectFtp();

    logger.info("2. 效验远程目录");
    logger.info("远程目录为:"+remoteFileDirectory);
    try {
        channelSftp.cd(remoteFileDirectory.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        mkdirs(remoteFileDirectory.replaceAll("\\\\","/"));
        return listFile;
    }

    logger.info("3. 查找文件名称");
    Vector<?> vector = null;
    try {
        vector = channelSftp.ls(remoteFileDirectory.replaceAll("\\\\","/"));
        for(Object obj : vector){
            if(obj != null){
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)obj;
                if(!entry.getAttrs().isDir()){
                    listFile.add(entry.getFilename());
                }
            }
        }
    } catch (SftpException e) {
        logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
        throw e;
    }finally {
        this.disConnect();
    }
    return listFile;
}	

形式2:

 /*
* 获取ftp服务器下某个文件下的  符合条件的文件
* */
public List<String> findConditionFiles(String sftpPath) throws Exception {

    //获取文件夹中的  所有符合条件文件
    logger.info("find directory files beginning...");
    List<String> listFile = new ArrayList<String>();
    logger.info("1. 连接ftp服务器");
    try {
        this.connectFtp();
    } catch (Exception e) {
        e.printStackTrace();
    }

    logger.info("2. 效验远程目录");
    logger.info("远程目录为:"+sftpPath);
    try {
        channelSftp.cd(sftpPath.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        mkdirs(sftpPath.replaceAll("\\\\","/"));
        return listFile;
    }

    logger.info("3. 查找文件名称");
    Vector<?> vector = null;
    try {
        vector = channelSftp.ls(sftpPath.replaceAll("\\\\","/"));
        for(Object obj : vector){
            if(obj != null){
                ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)obj;
                if(!entry.getAttrs().isDir()){
                    String filename = entry.getFilename();
                    if ("sendnumber_202".equals(filename.trim().substring(0,14))){
                        listFile.add(filename);
                        logger.info("有效CSV同步的文件为:"+filename);
                    }
                }
            }
        }
    } catch (SftpException e) {
        logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
        throw e;
    }finally {
        this.disConnect();
    }

    return listFile;
}

10.重命名ftp服务器上面的某个文件的名称

/**
 * @param srcFile     源文件
 * @param destFile    目标文件
 */
public void moveFile(String srcFile, String destFile) throws Exception{
    logger.info("1. 连接ftp服务器");
    this.connectFtp();

    logger.info("2. 效验目标文件的目录,如果没有则创建");
    int pos = destFile.lastIndexOf("/");
    this.mkdirs(destFile.substring(0,pos)); // 创建目录

    logger.info("3. 将源文件名称 重命名为 目标文件名称");
    try {
        channelSftp.rename(srcFile.replaceAll("\\\\","/"),destFile.replaceAll("\\\\","/"));
    } catch (SftpException e) {
        e.printStackTrace();
        logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
        throw e;
    }finally {
        this.disConnect();
    }
//      logger.info("4. 删除源文件");
//      this.ftpDelete(srcFile.replaceAll("\\\\","/"));
    }

至此,以上为使用Java操作sftp服务器的所有内容。

以下为相关的枚举类和整个工具类的完整代码。

FtpErrorCode枚举类:

package com.ai.prm.nms.scheduler.util;
public enum FtpErrorCode {
    CONNECT_SERVER_FAILER("连接sftp服务器失败"),
    DISCONNECT_SERVER_GET_CONNECT_FAILER("断开sftp服务器获取连接失败"),
    CREATE_DIRECTORY_FAILER("创建SFTP目录失败"),
    GET_LOCAL_FILE_INPUT_STREAM("获取本地文件输入流失败"),
    UPLOAD_FILE_FAILER("上传文件失败"),
    GET_LOCAL_FILE_OUTPUT_STREAM("获取本地输出流失败"),
    DELETE_FTP_FILE_FAILER("删除FTP文件失败"),
    CD_REMOTE_PATH_FAILER("进入远程目录失败"),
    LS_REMOTE_PATH_FAILER("进入远程目录失败");

    private String errorDesc;


    FtpErrorCode(String errorDesc) {
        this.errorDesc = errorDesc;
    }

    public String getErrorDesc() {
        return errorDesc;
    }

    public void setErrorDesc(String errorDesc) {
        this.errorDesc = errorDesc;
    }
}

SFTPUtils工具类

package com.ai.prm.nms.scheduler.util;

import com.jcraft.jsch.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

/**
 * 注意点:将所有远程目录都配置为 remoteFile.replaceAll("\\\\","/")
 */
public class SFTPUtils {
    private final static Logger logger = LoggerFactory.getLogger(FileService.class);

    private final static int CLIENT_TIMEOUT = 3600* 1000;

    ChannelSftp channelSftp = null;     //所有操作公用一个Channel
    private String serverIP;
    private Integer	port ;
    private String username;
    private String password;
    private String encoding = "UTF-8";

    public SFTPUtils(String serverIP, Integer port, String username, String password){
        this.serverIP = serverIP;
        if(port!=null){
            this.port = port;
        }
        this.username = username;
        this.password = password;
    }

    public static SFTPUtils newInstance(String serverIP, Integer port, String username, String password) {
        return new SFTPUtils(serverIP,port,username,password);
    }

    /**
     * (50+50) * 0.05 = 50* 0.05 + 50* 0.05
     * @return
     */
    public void connectFtp() throws Exception{
        logger.info("SftpUtils start beginning");
        Session session = null;
        try {
            JSch jSch = new JSch();
            session = jSch.getSession(username,serverIP,port);
            session.setTimeout(CLIENT_TIMEOUT);
            if(! StringUtils.isEmpty(password)){
                session.setPassword(password);
            }
            Properties properties = new Properties();
            properties.setProperty("StrictHostKeyChecking","no");
            session.setConfig(properties);
            session.connect();

            Channel channel = session.openChannel("sftp");
            channel.connect();

            channelSftp = (ChannelSftp)channel;

        }catch (Exception e){
            logger.info(FtpErrorCode.CONNECT_SERVER_FAILER.getErrorDesc());
            throw e;
        }
    }


    public void disConnect() throws Exception{
        logger.info("SftpUtils end beginning...");
        if(channelSftp != null){
            Session session= null;
            try {
                session = channelSftp.getSession();
            }catch (JSchException e){
                logger.info(FtpErrorCode.DISCONNECT_SERVER_GET_CONNECT_FAILER.getErrorDesc());
                throw e;
            }finally {
                if(session !=null){
                    session.disconnect();
                }
            }
        }
    }

    /**
     * 将本地文件上传到远程ftp服务器某目录中
     * @param localFile           本地文件全路径
     * @param remoteFile          远程文件全路径
     */
    public void uploadFile(String localFile, String remoteFile) throws Exception{
        logger.info("SftpUtils upload beginning");
        InputStream inputStream =null;
        logger.info("1. 连接ftp服务器");
        this.connectFtp();
        logger.info("2. 转换本地文件为输入流");
        try {
            inputStream = new FileInputStream(localFile);
        } catch (FileNotFoundException e) {
            this.disConnect();
            logger.info(FtpErrorCode.GET_LOCAL_FILE_INPUT_STREAM.getErrorDesc());
            throw e;
        }
        logger.info("3. 创建远程目录文件夹");
        String fileNameAbsolute = remoteFile.replaceAll("\\\\","/");
        String remoteDirectory = fileNameAbsolute.substring(0,fileNameAbsolute.lastIndexOf("/"));

        logger.info("remoteDirectory"+remoteDirectory);

        mkdirs(remoteDirectory);

        logger.info("4. 保存输入流到sftp服务器");
        try {
            channelSftp.put(inputStream,remoteFile.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            logger.info(FtpErrorCode.UPLOAD_FILE_FAILER.getErrorDesc());
            throw e;
        }finally {
            this.disConnect();
        }
    }

    /**
     * 将内存数据转换为输入流上传
     * @param localInputStream           本地输入流
     * @param remoteFile          远程文件全路径
     */
    public void uploadFile(InputStream localInputStream, String remoteFile) throws Exception{
        logger.info("SftpUtils upload beginning");
        logger.info("1. 连接ftp服务器");
        this.connectFtp();

        logger.info("2. 创建远程目录文件夹");
        String fileNameAbsolute = remoteFile.replaceAll("\\\\","/");
        String remoteDirectory = fileNameAbsolute.substring(0,fileNameAbsolute.lastIndexOf("/"));
        mkdirs(remoteDirectory);

        logger.info("4. 保存输入流到sftp服务器");
        try {
            channelSftp.put(localInputStream,remoteFile.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            logger.info(FtpErrorCode.UPLOAD_FILE_FAILER.getErrorDesc());
            throw e;
        }finally {
            this.disConnect();
        }
    }

    /**
     * 如果远程目录不存在,则创建远程目录
     * @param remoteDirectory               /abc/sdf/sdf/adf
     */
    void mkdirs(String remoteDirectory) throws Exception{
        try {
            channelSftp.cd(remoteDirectory);
        } catch (SftpException e) {
            String[] dirs = remoteDirectory.replaceAll("\\\\","/").split("/"); //{abc,sdf,sdf,adf}
            String tempPath = "";
            for(String dir : dirs){
                if(StringUtils.isEmpty(dir)){
                    continue;
                }

                //此处不能使用File.separator
                tempPath += "/" + dir;
                try {
                    channelSftp.cd(tempPath);
                } catch (SftpException sftpException) {
                    try {
                        channelSftp.mkdir(tempPath);
                        channelSftp.cd(tempPath);
                    } catch (Exception exception) {
                        this.disConnect();
                        logger.info(FtpErrorCode.CREATE_DIRECTORY_FAILER.getErrorDesc());
                        throw exception;
                    }
                }
            }
        }
        try {
            channelSftp.cd(remoteDirectory.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            this.disConnect();
            logger.info(FtpErrorCode.CONNECT_SERVER_FAILER.getErrorDesc());
            throw e;
        }
    }

    /**
     * @param remoteFile   远程文件————————>本系统特点是从一个目录取得
     * @param localFile    本地系统目录
     */
    public void ftpDownLoad(String remoteFile,String localFile) throws Exception{
        logger.info("SftpUtils download beginning");
        logger.info("1. 连接ftp服务器");
        this.connectFtp();
        logger.info("2. 创建本地文件输入流");
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(localFile);
        } catch (FileNotFoundException e) {
            this.disConnect();
            logger.info(FtpErrorCode.GET_LOCAL_FILE_OUTPUT_STREAM.getErrorDesc());
            throw new RuntimeException(e);
        }
        logger.info("3. 下载到本地文件/输出流");
        try {
            channelSftp.get(remoteFile.replaceAll("\\\\","/"),outputStream);
        } catch (SftpException e) {
            this.disConnect();
            logger.info(FtpErrorCode.GET_LOCAL_FILE_OUTPUT_STREAM.getErrorDesc());
            throw e;
        }finally {
            logger.info("4. 关闭sftp的连接");
            this.disConnect();
        }
    }


    //读取远程文件中的内容,并返回流  remoteFile:远程文件全路径 如:download/sds.csv
    public InputStream sftpWriter(String remoteFile){

        InputStream is=null;
        try {
            logger.info("SftpUtils download beginning");
            logger.info("1. 连接ftp服务器");
            this.connectFtp();
            logger.info("2. 创建远程文件输出流");
            is = channelSftp.get(remoteFile);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                logger.info("3. 关闭sftp的连接");
                this.disConnect();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return is;

    }


    public void ftpDelete(String remoteFile) throws Exception{
        logger.info("SftpUtils download beginning");
        logger.info("1. 连接ftp服务器");
        this.connectFtp();
        logger.info("2. 删除远端文件");
        String directory = remoteFile.replaceAll("\\\\","/").substring(0,remoteFile.lastIndexOf("/"));
        try {
            channelSftp.cd(directory);
        } catch (SftpException e) {
            this.disConnect();
            logger.info(FtpErrorCode.DELETE_FTP_FILE_FAILER.getErrorDesc());
            throw e;
        }
//        String fileName = remoteFile.substring(remoteFile.replaceAll("\\\\","/").lastIndexOf("/") + 1);
        try {
            channelSftp.rm(remoteFile);//remove()
        } catch (SftpException e) {
            this.disConnect();
            logger.info(FtpErrorCode.DELETE_FTP_FILE_FAILER.getErrorDesc());
            throw e;
        }finally {
            logger.info("4. 关闭sftp的连接");
            this.disConnect();
        }
    }

    /**
     * 查找目录下的所有文件
     * @param remoteFileDirectory
     * @return
     */
    public List<String> findFiles(String remoteFileDirectory) throws Exception{
        logger.info("find directory files beginning...");
        List<String> listFile = new ArrayList<String>();
        logger.info("1. 连接ftp服务器");
        this.connectFtp();

        logger.info("2. 效验远程目录");
        logger.info("远程目录为:"+remoteFileDirectory);
        try {
            channelSftp.cd(remoteFileDirectory.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            mkdirs(remoteFileDirectory.replaceAll("\\\\","/"));
            return listFile;
        }

        logger.info("3. 查找文件名称");
        Vector<?> vector = null;
        try {
            vector = channelSftp.ls(remoteFileDirectory.replaceAll("\\\\","/"));
            for(Object obj : vector){
                if(obj != null){
                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)obj;
                    if(!entry.getAttrs().isDir()){
                        listFile.add(entry.getFilename());
                    }
                }
            }
        } catch (SftpException e) {
            logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
            throw e;
        }finally {
            this.disConnect();
        }
        return listFile;
    }

    /*
    * 获取ftp服务器下某个文件下的  符合条件的文件
    * */
    public List<String> findConditionFiles(String sftpPath) throws Exception {

        //获取文件夹中的  所有符合条件文件
        logger.info("find directory files beginning...");
        List<String> listFile = new ArrayList<String>();
        logger.info("1. 连接ftp服务器");
        try {
            this.connectFtp();
        } catch (Exception e) {
            e.printStackTrace();
        }

        logger.info("2. 效验远程目录");
        logger.info("远程目录为:"+sftpPath);
        try {
            channelSftp.cd(sftpPath.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            mkdirs(sftpPath.replaceAll("\\\\","/"));
            return listFile;
        }

        logger.info("3. 查找文件名称");
        Vector<?> vector = null;
        try {
            vector = channelSftp.ls(sftpPath.replaceAll("\\\\","/"));
            for(Object obj : vector){
                if(obj != null){
                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry)obj;
                    if(!entry.getAttrs().isDir()){
                        String filename = entry.getFilename();
                        if ("sendnumber_202".equals(filename.trim().substring(0,14))){
                            listFile.add(filename);
                            logger.info("有效CSV同步的文件为:"+filename);
                        }
                    }
                }
            }
        } catch (SftpException e) {
            logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
            throw e;
        }finally {
            this.disConnect();
        }

        return listFile;
    }

    /**
     * @param srcFile     源文件
     * @param destFile    目标文件
     */
    public void moveFile(String srcFile, String destFile) throws Exception{
        logger.info("1. 连接ftp服务器");
        this.connectFtp();

        logger.info("2. 效验目标文件的目录,如果没有则创建");
        int pos = destFile.lastIndexOf("/");
        this.mkdirs(destFile.substring(0,pos)); // 创建目录

        logger.info("3. 将源文件名称 重命名为 目标文件名称");
        try {
            channelSftp.rename(srcFile.replaceAll("\\\\","/"),destFile.replaceAll("\\\\","/"));
        } catch (SftpException e) {
            e.printStackTrace();
            logger.info(FtpErrorCode.LS_REMOTE_PATH_FAILER.getErrorDesc());
            throw e;
        }finally {
            this.disConnect();
        }
//      logger.info("4. 删除源文件");
//      this.ftpDelete(srcFile.replaceAll("\\\\","/"));
    }

}

版权声明:本文为MyBlogHiHi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。