软件工具——java开发sftp客户端上传下载文件

版权声明:原创博文,转载请注明出处 https://blog.csdn.net/qq_15903671/article/details/88736626

上一篇博文:https://blog.csdn.net/qq_15903671/article/details/88681240

部署了一个linux的sftp-server。客户端工具众多且不受操作系统限制,但是想灵活的制作文件上传下载流程、定时触发、文件解析转存等操作,客户端工具就可能由于功能不够完整而使用不便了。

下面使用java语言做一个简单的sftp客户端操作工具来灵活的使用sftp。

开发环境:windows、IDEA、maven、java

一、引入maven依赖

在idea工程里找到pom.xml ,添加dependence标签

<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<!-- sftp文件传输相关依赖-->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.48</version>
</dependency>

使用jsch依赖。

二、网上下载个操作类源码

package sshUtils;

import com.jcraft.jsch.*;

import java.io.*;
import java.util.*;

public class SFTPUtils {
    //private static Logger log = Logger.getLogger(SFTPUtils.class.getName());

    private String host;//服务器连接ip
    private String username;//用户名
    private String password;//密码
    private int port = 22;//端口号
    private ChannelSftp sftp = null;
    private Session sshSession = null;

    public SFTPUtils() {
    }

    public SFTPUtils(String host, int port, String username, String password) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
    }

    public SFTPUtils(String host, String username, String password) {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    /**
     * 通过SFTP连接服务器
     */
    public void connect() {
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            //System.out.println("Session created.");
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            //System.out.println("Session connected.");
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            //System.out.println("Opening Channel.");
            sftp = (ChannelSftp) channel;
            System.out.println("Connected to " + host + " success.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void disconnect() {
        if (this.sftp != null) {
            if (this.sftp.isConnected()) {
                this.sftp.disconnect();
                System.out.println("sftp is closed already");
            }
        }
        if (this.sshSession != null) {
            if (this.sshSession.isConnected()) {
                this.sshSession.disconnect();
                System.out.println("sshSession is closed already");
            }
        }
    }

    /**
     * 批量下载文件
     *
     * @param remotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)
     * @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\)
     * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
     * @param fileEndFormat:下载文件格式(文件格式)
     * @param del:下载后是否删除sftp文件
     * @return
     */
    public List<String> batchDownLoadFile(String remotePath, String localPath,
                                          String fileFormat, String fileEndFormat, boolean del) {
        List<String> filenames = new ArrayList<String>();
        try {
            // connect();
            Vector v = listFiles(remotePath);
            // sftp.cd(remotePath);
            if (v.size() > 0) {
                System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
                Iterator it = v.iterator();
                while (it.hasNext()) {
                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) it.next();
                    String filename = entry.getFilename();
                    SftpATTRS attrs = entry.getAttrs();
                    if (!attrs.isDir()) {
                        boolean flag = false;
                        String localFileName = localPath + filename;
                        fileFormat = fileFormat == null ? "" : fileFormat
                                .trim();
                        fileEndFormat = fileEndFormat == null ? ""
                                : fileEndFormat.trim();
                        // 三种情况
                        if (fileFormat.length() > 0 && fileEndFormat.length() > 0) {
                            if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat)) {
                                flag = downloadFile(remotePath, filename, localPath, filename);
                                if (flag) {
                                    filenames.add(localFileName);
                                    if (flag && del) {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        } else if (fileFormat.length() > 0 && "".equals(fileEndFormat)) {
                            if (filename.startsWith(fileFormat)) {
                                flag = downloadFile(remotePath, filename, localPath, filename);
                                if (flag) {
                                    filenames.add(localFileName);
                                    if (flag && del) {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        } else if (fileEndFormat.length() > 0 && "".equals(fileFormat)) {
                            if (filename.endsWith(fileEndFormat)) {
                                flag = downloadFile(remotePath, filename, localPath, filename);
                                if (flag) {
                                    filenames.add(localFileName);
                                    if (flag && del) {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        } else {
                            flag = downloadFile(remotePath, filename, localPath, filename);
                            if (flag) {
                                filenames.add(localFileName);
                                if (flag && del) {
                                    deleteSFTP(remotePath, filename);
                                }
                            }
                        }
                    }
                }
            }
            System.out.println("download file is success:remotePath=" + remotePath
                    + "and localPath=" + localPath + ",file size is"
                    + v.size());
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            // this.disconnect();
        }
        return filenames;
    }

    /**
     * 下载单个文件
     *
     * @param remotePath:远程下载目录(以路径符号结束)
     * @param remoteFileName:下载文件名
     * @param localPath:本地保存目录(以路径符号结束)
     * @param localFileName:保存文件名
     * @return
     */
    public boolean downloadFile(String remotePath, String remoteFileName, String localPath, String localFileName) {
        FileOutputStream fieloutput = null;
        try {
            // sftp.cd(remotePath);
            File file = new File(localPath + localFileName);
            mkdirs(localPath + localFileName);
            fieloutput = new FileOutputStream(file);
            sftp.get(remotePath + remoteFileName, fieloutput);
            System.out.println("===DownloadFile:" + remoteFileName + " success from sftp.");
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        } finally {
            if (null != fieloutput) {
                try {
                    fieloutput.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 上传单个文件
     *
     * @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;
    }

    /**
     * 批量上传文件
     *
     * @param remotePath:远程保存目录
     * @param localPath:本地上传目录(以路径符号结束)
     * @param del:上传后是否删除本地文件
     * @return
     */
    public boolean bacthUploadFile(String remotePath, String localPath,
                                   boolean del) {
        try {
            connect();
            File file = new File(localPath);
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()
                        && files[i].getName().indexOf("bak") == -1) {
                    if (this.uploadFile(remotePath, files[i].getName(),
                            localPath, files[i].getName())
                            && del) {
                        deleteFile(localPath + files[i].getName());
                    }
                }
            }
            System.out.println("upload file is success:remotePath=" + remotePath
                    + "and localPath=" + localPath + ",file size is "
                    + files.length);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.disconnect();
        }
        return false;

    }

    /**
     * 删除本地文件
     *
     * @param filePath
     * @return
     */
    public boolean deleteFile(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            return false;
        }

        if (!file.isFile()) {
            return false;
        }
        boolean rs = file.delete();
        if (rs)
            System.out.println("delete file success from local.");
        else System.out.println("delete file failed from local.");
        return rs;
    }

    /**
     * 创建目录
     *
     * @param createpath
     * @return
     */
    public boolean createDir(String createpath) {
        try {
            if (isDirExist(createpath)) {
                this.sftp.cd(createpath);
                return true;
            }
            String pathArry[] = createpath.split("/");
            StringBuffer filePath = new StringBuffer("/");
            for (String path : pathArry) {
                if (path.equals("")) {
                    continue;
                }
                filePath.append(path + "/");
                if (isDirExist(filePath.toString())) {
                    sftp.cd(filePath.toString());
                } else {
                    // 建立目录
                    sftp.mkdir(filePath.toString());
                    // 进入并设置为当前目录
                    sftp.cd(filePath.toString());
                }

            }
            this.sftp.cd(createpath);
            return true;
        } catch (SftpException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断目录是否存在
     *
     * @param directory
     * @return
     */
    public boolean isDirExist(String directory) {
        boolean isDirExistFlag = false;
        try {
            SftpATTRS sftpATTRS = sftp.lstat(directory);
            isDirExistFlag = true;
            return sftpATTRS.isDir();
        } catch (Exception e) {
            if (e.getMessage().toLowerCase().equals("no such file")) {
                isDirExistFlag = false;
            }
        }
        return isDirExistFlag;
    }

    /**
     * 删除stfp文件
     *
     * @param directory:要删除文件所在目录
     * @param deleteFile:要删除的文件
     */
    public void deleteSFTP(String directory, String deleteFile) {
        try {
            // sftp.cd(directory);
            sftp.rm(directory + deleteFile);
            System.out.println("delete file success from sftp.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 如果目录不存在就创建目录
     *
     * @param path
     */
    public void mkdirs(String path) {
        File f = new File(path);

        String fs = f.getParent();

        f = new File(fs);

        if (!f.exists()) {
            f.mkdirs();
        }
    }

    /**
     * 列出目录下的文件
     *
     * @param directory:要列出的目录
     * @return
     * @throws SftpException
     */
    public Vector listFiles(String directory) throws SftpException {
        return sftp.ls(directory);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public ChannelSftp getSftp() {
        return sftp;
    }

    public void setSftp(ChannelSftp sftp) {
        this.sftp = sftp;
    }
}

这个类的源码是从网上扒下来的。

三、做个配置读取类

3.1 配置文件 config.txt

sftp_host = 172.30.86.144 #sftp服务器ip
sftp_user = sftpuser1  #用户名
sftp_pswd = ****  #密码
sftp_port = 22  #端口

sftp_filedownload_remotePath = /
sftp_filedownload_localPath = D:\testFile\sftp\BloombergDownloadFile\
sftp_filedownload_fileFormat =
sftp_filedownload_fileEndFormat = .csv
sftp_filedownload_del = false

3.2 配置文件读取工具类

package BloombergData;

import FileExtractTxt.ParseText;

import java.io.File;

public class ConfModel {

    public static String sftp_host = "";
    public static String sftp_user = "";
    public static String sftp_pswd = "";
    public static String sftp_port = "";

    public static String sftp_filedownload_remotePath = "";
    public static String sftp_filedownload_localPath = "";
    public static String sftp_filedownload_fileFormat = "";
    public static String sftp_filedownload_fileEndFormat = "";
    public static boolean sftp_filedownload_del = false;

    static{
        if((new File("conf.txt")).exists())init("conf.txt");
    }

    public static String init(String conf_file){
        if(!(new File(conf_file)).exists()){
            System.out.println("找不到配置文件[" + conf_file + "]");
            return "找不到配置文件[" + conf_file + "]";//如果找不到配置文件,直接退出。
        }
        String conf_data = "";
        try {
            conf_data = new ParseText().parseAllString(conf_file);
            String[] list = conf_data.split("\n");

            for(String d:list){
                if(d.indexOf("sftp_host")==0)sftp_host = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_user")==0)sftp_user = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_pswd")==0)sftp_pswd = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_port")==0)sftp_port = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();

                if(d.indexOf("sftp_filedownload_remotePath")==0)sftp_filedownload_remotePath = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_filedownload_localPath")==0)sftp_filedownload_localPath = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_filedownload_fileFormat")==0)sftp_filedownload_fileFormat = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_filedownload_fileEndFormat")==0)sftp_filedownload_fileEndFormat = d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim();
                if(d.indexOf("sftp_filedownload_del")==0)sftp_filedownload_del = "TRUE".equalsIgnoreCase(d.substring(d.indexOf("=")+1,d.indexOf("#")<0?d.length():d.indexOf("#")).trim())?true:false;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}

四、测试用例

package BloombergData;

import sshUtils.SFTPUtils;

import java.util.List;

public class Main {

    public static SFTPUtils sftpUtils ;
    public static void main (String [] args){
        ConfModel.init("BloombergConfig.txt");
        //System.out.println("host:" + ConfModel.sftp_host);
        //System.out.println("user:" + ConfModel.sftp_user);
        //System.out.println("pswd:" + ConfModel.sftp_pswd);
        //System.out.println("port:" + ConfModel.sftp_port);
        sftpUtils = new SFTPUtils(ConfModel.sftp_host
                ,Integer.valueOf(ConfModel.sftp_port)
                ,ConfModel.sftp_user
                ,ConfModel.sftp_pswd);
        sftpUtils.connect();

        List<String> filenames = sftpUtils.batchDownLoadFile(ConfModel.sftp_filedownload_remotePath
                ,ConfModel.sftp_filedownload_localPath
                ,ConfModel.sftp_filedownload_fileFormat
                ,ConfModel.sftp_filedownload_fileEndFormat
                ,ConfModel.sftp_filedownload_del);
        for(String filename:filenames)System.out.println("downlaod file:" + filename);

        sftpUtils.disconnect();

    }
}

猜你喜欢

转载自blog.csdn.net/qq_15903671/article/details/88736626