Sftp工具类

相关jar包配置

     <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
     </dependency>
package com.roof.common.util;

import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * 文件工具类.<br>
 * 0.【注意】所有涉及服务器上文件目录路径的地方均为服务器上绝对路径.
 * 1.所有的文件路径必须以'/'开头和结尾,否则路径最后一部分会被当做是文件名<br>
 * 2. @since version-0.3 方法出现异常的时候,<del>会关闭sftp连接(但是不会关闭session和channel)</del>(del @ version 0.31),异常会抛出<br>
 * @author Leon Lee
 */
public class SftpUtil {

    /**
     * 文件路径前缀. /ddit-remote
     */
    private static final String PRE_FIX = "";

    /**
     * sftp连接池.
     */
    private static final Map<String, Channel> SFTP_CHANNEL_POOL = new HashMap<String, Channel>();

    /**
     * 获取sftp协议连接.
     * @param host 主机名
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @return 连接对象
     * @throws JSchException 异常
     */
    public static ChannelSftp getSftpConnect(final String host, final int port, final String username,
                                             final String password) throws JSchException {
        Session sshSession = null;
        Channel channel = null;
        ChannelSftp sftp = null;
        JSch jsch = new JSch();
        jsch.getSession(username, host, port);
        sshSession = jsch.getSession(username, host, port);
        sshSession.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        sshSession.setConfig(sshConfig);
        sshSession.connect();
        channel = sshSession.openChannel("sftp");
        channel.connect();
        sftp = (ChannelSftp) channel;
        return sftp;
    }

    /**
     * 关闭sftp连接
     * @param sftp
     * @throws JSchException
     */
    public static void disConnectSftp(final ChannelSftp sftp) throws JSchException {
        Session sshSession =  sftp.getSession();
        if (sshSession!=null&&sshSession.isConnected()){
            sshSession.disconnect();
        }
        if (sftp!=null&&sftp.isConnected()){
            sftp.disconnect();
        }
    }

    /**
     * 下载文件-sftp协议.
     * @param downloadFile 下载的文件
     * @param saveFile 存在本地的路径
     * @param sftp sftp连接
     * @return 文件
     * @throws Exception 异常
     */
    public static File download(final String downloadFile, final String saveFile, final ChannelSftp sftp)
            throws Exception {
        FileOutputStream os = null;
        File file = new File(saveFile);
        try {
            if (!file.exists()) {
                File parentFile = file.getParentFile();
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                file.createNewFile();
            }
            os = new FileOutputStream(file);
            List<String> list = formatPath(downloadFile);
            sftp.get(list.get(0) + list.get(1), os);
        } catch (Exception e) {
            throw e;
        } finally {
            if (os!=null){
                os.close();
            }
        }
        return file;
    }

    /**
     * 下载文件-sftp协议.
     * @param downloadFile 下载的文件
     * @param sftp sftp连接
     * @return 文件 byte[]
     * @throws Exception 异常
     */
    public static byte[] downloadAsByte(final String downloadFile, final ChannelSftp sftp) throws Exception {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            List<String> list = formatPath(downloadFile);
            sftp.get(list.get(0) + list.get(1), os);
        } catch (Exception e) {
            throw e;
        } finally {
            if (os!=null){
                os.close();
            }
        }
        return os.toByteArray();
    }

    /**
     * 删除文件-sftp协议.
     * @param pathString 要删除的文件
     * @param sftp sftp连接
     * @throws SftpException 异常
     */
    public static void rmFile(final String pathString, final ChannelSftp sftp) throws SftpException {
        List<String> list = formatPath(pathString);
        String dir = list.get(0);
        String file = list.get(1);
        if (dirExist(dir + file, sftp)) {
            sftp.rm(list.get(0) + list.get(1));
        }
    }

    /**
     * 删除文件夹-sftp协议.如果文件夹有内容,则会抛出异常.
     * @param pathString 文件夹路径
     * @param sftp sftp连接
     * @param recursion 递归删除
     * @throws SftpException 异常
     */
    public static void rmDir(final String pathString, final ChannelSftp sftp, final boolean recursion)
            throws SftpException {
        String fp = formatPath(pathString).get(0);
        if (dirExist(fp, sftp)) {
            if (recursion){
                exeRmRec(fp, sftp);
            }
            else {
                sftp.rmdir(fp);
            }
        }
    }

    /**
     * 递归删除执行.
     * @param pathString 文件路径
     * @param sftp sftp连接
     * @throws SftpException
     */
    private static void exeRmRec(final String pathString, final ChannelSftp sftp) throws SftpException {
        @SuppressWarnings("unchecked")
        Vector<LsEntry> vector = sftp.ls(pathString);
        if (vector.size() == 1) { // 文件,直接删除
            sftp.rm(pathString);
        } else if (vector.size() == 2) { // 空文件夹,直接删除
            sftp.rmdir(pathString);
        } else {
            String fileName = "";
            // 删除文件夹下所有文件
            for (LsEntry en : vector) {
                fileName = en.getFilename();
                if (".".equals(fileName) || "..".equals(fileName)) {
                    continue;
                } else {
                    exeRmRec(pathString + "/" + fileName, sftp);
                }
            }
            // 删除文件夹
            sftp.rmdir(pathString);
        }
    }

    /**
     * 上传文件-sftp协议.
     * @param srcFile 源文件
     * @param dir 保存路径
     * @param fileName 保存文件名
     * @param sftp sftp连接
     * @throws Exception 异常
     */
    private static void uploadFile(final String srcFile, final String dir, final String fileName,
                                   final ChannelSftp sftp) throws SftpException {
        mkdir(dir, sftp);
        sftp.cd(dir);
        sftp.put(srcFile, fileName);
    }

    /**
     * 上传文件-sftp协议.
     * @param srcFile 源文件路径,/xxx/xx.yy 或 x:/xxx/xxx.yy
     * @param sftp sftp连接
     * @return 上传成功与否
     * @throws SftpException 异常
     */
    public static boolean uploadFile(final String srcFile, final String dir, final ChannelSftp sftp) throws SftpException {
        File file = new File(srcFile);
        if (file.exists()) {
            List<String> list = formatPath(srcFile);
            uploadFile(srcFile, dir, list.get(1), sftp);
            return true;
        }
        return false;
    }

    /**
     * 根据路径创建文件夹.
     * @param dir 路径 必须是 /xxx/xxx/ 不能就单独一个/
     * @param sftp sftp连接
     * @throws SftpException 异常
     */
    public static boolean mkdir(final String dir, final ChannelSftp sftp) throws SftpException {
        if (StringUtils.isBlank(dir)) {
            return false;
        }
        String md = dir.replaceAll("\\\\", "/");
        if (md.indexOf("/") != 0 || md.length() == 1) {
            return false;
        }
        return mkdirs(md, sftp);
    }

    /**
     * 递归创建文件夹.
     * @param dir 路径
     * @param sftp sftp连接
     * @return 是否创建成功
     * @throws SftpException 异常
     */
    private static boolean mkdirs(final String dir, final ChannelSftp sftp) throws SftpException {
        String dirs = dir.substring(1, dir.length() - 1);
        String[] dirArr = dirs.split("/");
        String base = "";
        for (String d : dirArr) {
            base += "/" + d;
            if (dirExist(base + "/", sftp)) {
                continue;
            } else {
                sftp.mkdir(base + "/");
            }
        }
        return true;
    }

    /**
     * 判断文件夹是否存在.
     * @param dir 文件夹路径, /xxx/xxx/
     * @param sftp sftp协议
     * @return 是否存在
     */
    private static boolean dirExist(final String dir, final ChannelSftp sftp) {
        try {
            Vector<?> vector = sftp.ls(dir);
            if (null == vector) {
                return false;
            }
            else {
                return true;
            }
        } catch (SftpException e) {
            return false;
        }
    }

    /**
     * 格式化路径.
     * @param srcPath 原路径. /xxx/xxx/xxx.yyy 或 X:/xxx/xxx/xxx.yy
     * @return list, 第一个是路径(/xxx/xxx/),第二个是文件名(xxx.yy)
     */
    public static List<String> formatPath(final String srcPath) {
        List<String> list = new ArrayList<String>(2);
        String repSrc = srcPath.replaceAll("\\\\", "/");
        int firstP = repSrc.indexOf("/");
        int lastP = repSrc.lastIndexOf("/");
        String fileName = lastP + 1 == repSrc.length() ? "" : repSrc.substring(lastP + 1);
        String dir = firstP == -1 ? "" : repSrc.substring(firstP, lastP);
        dir = (dir.length() == 1 ? dir : (dir + "/"));
        list.add(dir);
        list.add(fileName);
        return list;
    }

    /**
     * 关闭协议-sftp协议.(关闭会导致连接池异常,因此不建议用户自定义关闭)
     * @param sftp sftp连接
     */
    private static void exit(final ChannelSftp sftp) {
        sftp.exit();
    }

    /**
     * 列出目录下的文件 
     *
     * @param directory
     *            要列出的目录 
     * @param sftp
     * @return LsEntry List, 包含.和.., 随机排序
     * @throws SftpException
     */
    public static Vector<LsEntry> listFiles(String directory, final ChannelSftp sftp) throws SftpException {
        return sftp.ls(directory);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值