2021-06-08 java远程操作文件demo

package com.example.jsch_smb;

import cn.hutool.core.io.FileUtil;

import cn.hutool.extra.ssh.JschUtil;

import cn.hutool.extra.ssh.Sftp;

import com.jcraft.jsch.*;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;

import java.util.List;

@Slf4j

public class JschDemo {

/**

maven依赖

   

    cn.hutool

    hutool-all

    5.6.3

    com.jcraft

    jsch

    0.1.53

    */

    private final static String[]IPS =new String[]{"10.197.227.245"};

    private final static IntegerPORT =55555;

    private final static StringSSHUSER ="root";

    private final static StringSSHPASS ="admin123";

    private static Sessionsession =null;

    private static Sftpsftp =null;

    private static ChannelSftpchannelSftp =null;

    public static void main(String[] args)throws Exception {

//创建session

        connect();

        //删除文件

        delRecursiveFile("/aidd/nginx/nginx/html/");

        //本地上传文件

        uploadFile("C:\\Users\\zhuhai8\\Desktop\\jar\\webhtml\\distyy.zip", "/aidd/nginx/nginx/html/dist.zip");

        //执行cmd

        String command =" cd /aidd/nginx/nginx/html && unzip dist.zip && mv dist/* . ";

        execCmd(command);

        //关闭session

        disconnect();

    }

private static void execCmd(String cmd)throws Exception {

ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

        InputStream in = channelExec.getInputStream();

        channelExec.setCommand(cmd);

        //channelExec.setErrStream(System.err);

        InputStream err = channelExec.getErrStream();

        channelExec.connect();

        //打印日志

        byte[] br =new byte[1024];

        StringBuffer bufferr =new StringBuffer();

        while (in.read(br) >0) {

bufferr.append(new String(br));

        }

log.info("成功的处理返回============={}", bufferr.toString());

        byte[] be =new byte[1024];

        StringBuffer buffere =new StringBuffer();

        while (err.read(be) >0) {

buffere.append(new String(be));

        }

log.info("异常的处理返回============={}", buffere.toString());

        //释放资源

        err.close();

        in.close();

        channelExec.disconnect();

    }

private static void connect() {

JSch jsch =new JSch();

        try {

session = jsch.getSession(SSHUSER, IPS[0], PORT);

            session.setPassword(SSHPASS);

            session.setConfig("StrictHostKeyChecking", "no");

            session.setTimeout(6000);

            session.connect();

            sftp = JschUtil.createSftp(session);

            channelSftp = JschUtil.openSftp(session);

        }catch (JSchException e) {

e.printStackTrace();

        }

}

private static void disconnect() {

if (sftp !=null) {

sftp.close();

        }

if (channelSftp !=null) {

channelSftp.disconnect();

        }

if (session !=null) {

session.disconnect();

        }

}

private static ListlsFileAndDirName(String path) {

return sftp.ls(path);

    }

private static void delRecursiveFile(String path) {

List strings =lsFileAndDirName(path);

        for (String string : strings) {

String filePath = path + string;

            try {

SftpATTRS lstat =channelSftp.lstat(filePath);

                if (lstat !=null) {

if (lstat.isDir()) {

sftp.delDir(filePath);

                    }else {

sftp.delFile(filePath);

                    }

}

}catch (SftpException e) {

e.printStackTrace();

            }

}

}

private static void uploadFile(String localPath, String destPath) {

sftp.put(localPath, destPath);

    }

private static void downloadFile(String sourcePath, String newFilePath) {

sftp.download(sourcePath, FileUtil.file(newFilePath));

    }

}

你可能感兴趣的:(2021-06-08 java远程操作文件demo)