使用Jsch进行文件传输及执行命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/devilcry13/article/details/83995085

sftp文件传输:

package com.ljl.fs.utils;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
 
import org.apache.commons.lang.StringUtils;
 
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
 
/**
 * 文件工具类.
  * 1.所有的文件路径必须以'/'开头和结尾,否则路径最后一部分会被当做是文件名
  * 2.方法出现异常的时候,会关闭sftp连接(但是不会关闭session和channel),异常会抛出
 */
public class SftpUtil {
 
    /**
     * 文件路径前缀
     */
    private static final String PRE_FIX = "/sftp-preffix";
 
    /**
     * 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;
        String key = host + "," + port + "," + username + "," + password;
        if (null == SFTP_CHANNEL_POOL.get(key)) {
            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_CHANNEL_POOL.put(key, channel);
        } else {
            channel = SFTP_CHANNEL_POOL.get(key);
            sshSession = channel.getSession();
            if (!sshSession.isConnected())
                sshSession.connect();
            if (!channel.isConnected())
                channel.connect();
        }
        sftp = (ChannelSftp) channel;
        return sftp;
    }
 
    /**
     * 下载文件-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) {
            exit(sftp);
            e.getMessage();
            throw e;
        } finally {
            os.close();
        }
        return file;
    }
 
    /**
     * 下载文件-sftp协议.
     * @param downloadFile 下载的文件
     * @param saveFile 存在本地的路径
     * @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) {
            exit(sftp);
            throw e;
        } finally {
            os.close();
        }
        return os.toByteArray();
    }
 
    /**
     * 删除文件-sftp协议.
     * @param pathString 要删除的文件
     * @param sftp sftp连接
     * @throws Exception 异常
     */
    public static void rmFile(final String pathString, final ChannelSftp sftp) throws Exception {
        try {
            List<string> list = formatPath(pathString);
            sftp.rm(list.get(0) + list.get(1));
        } catch (Exception e) {
            exit(sftp);
            throw e;
        }
    }
 
    /**
     * 删除文件夹-sftp协议.如果文件夹有内容,则会抛出异常.
     * @param pathString 文件夹路径
     * @param sftp sftp连接
     * @param resursion 递归删除
     * @throws Exception 异常
     */
    public static void rmDir(final String pathString, final ChannelSftp sftp, final boolean recursion) throws Exception {
        try {
            String fp = formatPath(pathString).get(0);
            if (recursion)
                exeRmRec(fp, sftp);
            else
                sftp.rmdir(fp);
        } catch (Exception e) {
            exit(sftp);
            throw e;
        }
    }
 
    /**
     * 递归删除执行.
     * @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 Exception {
        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 Exception 异常
     */
    public static boolean uploadFile(final String srcFile, final ChannelSftp sftp) throws Exception {
        try {
            File file = new File(srcFile);
            if (file.exists()) {
                List<string> list = formatPath(srcFile);
                uploadFile(srcFile, list.get(0), list.get(1), sftp);
                return true;
            }
            return false;
        } catch (Exception e) {
            exit(sftp);
            throw e;
        }
    }
 
    /**
     * 根据路径创建文件夹.
     * @param dir 路径 必须是 /xxx/xxx/ 不能就单独一个/
     * @param sftp sftp连接
     * @throws Exception 异常
     */
    public static boolean mkdir(final String dir, final ChannelSftp sftp) throws Exception {
        try {
            if (StringUtils.isBlank(dir))
                return false;
            String md = dir.replaceAll("\\\\", "/");
            if (md.indexOf("/") != 0 || md.length() == 1)
                return false;
            return mkdirs(md, sftp);
        } catch (Exception e) {
            exit(sftp);
            throw e;
        }
    }
 
    /**
     * 递归创建文件夹.
     * @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 = PRE_FIX + (dir.length() == 1 ? dir : (dir + "/"));
        list.add(dir);
        list.add(fileName);
        return list;
    }
 
    /**
     * 关闭协议-sftp协议.
     * @param sftp sftp连接
     */
    public static void exit(final ChannelSftp sftp) {
        sftp.exit();
    }
 
    public static void main(String[] args) throws Exception {
        ChannelSftp sftp = getSftpConnect("192.168.0.35", 22, "root", "root");
        // String pathString = "C:\\test\\ccc\\Foxmail7.zip";
        // File file = new File(pathString);
        // System.out.println("上传文件开始...");
        // uploadFile(pathString, sftp);
        // System.out.println("上传成功,开始删除本地文件...");
        // file.delete();
        // System.out.println("删除完成,开始校验本地文件...");
        // if (!file.exists()) {
        // System.out.println("文件不存在,开始从远程服务器获取...");
        // download(pathString, pathString, sftp);
        // System.out.println("下载完成");
        // } else {
        // System.out.println("在本地找到文件");
        // }
        rmDir("", sftp, true);
        exit(sftp);
        System.exit(0);
    }
}

执行ssh命令:

package com.ljl.fs.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class JSchUtils {
    private static JSch jsch;
    private static Session session;

    /**
     * 连接到指定的IP
     *
     * @throws JSchException
     */
    public static void connectByPassword(String user, String passwd, String host, int port) throws JSchException {
        jsch = new JSch();// 创建JSch对象
        session = jsch.getSession(user, host, port);// 根据用户名、主机ip、端口号获取一个Session对象

        session.setPassword(passwd);// 设置密码

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);// 为Session对象设置properties
        session.setTimeout(1500);// 设置超时
        session.connect();// 通过Session建立连接
    }

    /**
     * 连接到指定的IP
     *
     * @throws JSchException
     */
    public static void connectByPvk(String user, String keyPath, String host, int port) throws JSchException {
        jsch = new JSch();// 创建JSch对象

        jsch.addIdentity(keyPath);// 设置私钥
        session = jsch.getSession(user, host, port);// 根据用户名、主机ip、端口号获取一个Session对象

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);// 为Session对象设置properties
        session.setTimeout(1500);// 设置超时
        session.connect();// 通过Session建立连接
    }


    /**
     * 关闭连接
     */
    public static void close() {
        session.disconnect();
    }

    /**
     * 执行相关的命令
     *
     * @throws JSchException
     */
    public static void execCmd(String command) throws JSchException {
        BufferedReader reader = null;
        Channel channel = null;
        try {
            if (command != null) {
                channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(command);
                // ((ChannelExec) channel).setErrStream(System.err);
                channel.connect();

                InputStream in = channel.getInputStream();
                reader = new BufferedReader(new InputStreamReader(in));
                String buf = null;
                while ((buf = reader.readLine()) != null) {
                    System.out.println(buf);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSchException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            channel.disconnect();
        }
    }

    /**
     * 上传文件
     *
     * @param directory
     *            上传的目录
     * @param uploadFile
     *            要上传的文件
     * @param sftp
     * @throws JSchException
     * @throws SftpException
     * @throws FileNotFoundException
     */
    public void upload(String directory, String uploadFile) throws JSchException, FileNotFoundException, SftpException {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.cd(directory);
        File file = new File(uploadFile);
        channelSftp.put(new FileInputStream(file), file.getName());
        System.out.println("Upload Success!");
    }

    /**
     * 下载文件
     *
     * @param src
     * @param dst
     * @throws JSchException
     * @throws SftpException
     */
    public static void download(String src, String dst) throws JSchException, SftpException {
        // src linux服务器文件地址,dst 本地存放地址
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        channelSftp.get(src, dst);
        channelSftp.quit();
    }

    /**
     * 删除文件
     *
     * @param directory
     *            要删除文件所在目录
     * @param deleteFile
     *            要删除的文件
     * @param sftp
     * @throws SftpException
     * @throws JSchException
     */
    public void delete(String directory, String deleteFile) throws SftpException, JSchException {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.cd(directory);
        channelSftp.rm(deleteFile);
    }

    /**
     * 列出目录下的文件
     *
     * @param directory
     *            要列出的目录
     * @param sftp
     * @return
     * @throws SftpException
     * @throws JSchException
     */
    @SuppressWarnings("rawtypes")
    public Vector listFiles(String directory) throws JSchException, SftpException {
        ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
        return channelSftp.ls(directory);
    }

    public static void main(String[] args) {
        try {
            // 1.连接到指定的服务器
			// connectByPassword("userName", "123456", "192.168.43.128", 22);
            connectByPvk("userName", "D:\\test\\baoLeiJi\\id_rsa_2048_ljl", "192.168.43.128", 22);

            // 2.执行相关的命令
			// execCmd("grep 'ERROR' /export/App/www.xxx.com/logs/catalina.out >> ~/greped_keyword.log");
            execCmd("ifconfig;ls");

            // 3.下载文件
			// download("/home/userName/Downloads/Python-2.7.8.tgz", "D:\\Python-2.7.8.tgz");
            download("/upload/SCF_20180413_020000.csv", "D:\\SCF_20180413_020000.csv");

            // 4.关闭连接
            close();
        } catch (JSchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SftpException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/devilcry13/article/details/83995085
今日推荐