首页>代码>Java连接Linux服务器并上传文件、下载文件、发送指令>/new_maven/src/main/java/com/hai/util/sftpConnectionUtil.java
package com.hai.util;

import com.jcraft.jsch.*;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class sftpConnectionUtil {

    //连接服务器
    public static Session connect(String host, int port, String username, String password){
        Session sshSession = null;
        try {
            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();
        }catch (Exception e){
            e.printStackTrace();
        }
        return sshSession;

    }

    //连接sftp服务器
    public static ChannelSftp sftpConnect(Session sshSession){

        ChannelSftp sftp = null;
        try {
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;

        }catch (Exception e){
            e.printStackTrace();
        }
        return sftp;

    }

    //连接exec服务器
    public static ChannelExec execConnect(Session sshSession){

        ChannelExec channelExec = null;
        try {
            channelExec = (ChannelExec) sshSession.openChannel("exec");

        }catch (Exception e){
            e.printStackTrace();
        }

        return channelExec;

    }

    /**
     * 上传文件
     *
     * @param directory 上传的目录
     * @param uploadFile 要上传的文件
     * @param sftp
     */
    public static void upload(String directory, String uploadFile, ChannelSftp sftp){
        try {
            sftp.cd(directory);
            File file = new File(uploadFile);
            sftp.put(new FileInputStream(file), file.getName());
            System.out.println("上传成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 下载文件
     *
     * @param directory 下载目录
     * @param downloadFile 下载的文件
     * @param saveFile 存在本地的路径
     * @param sftp
     */
    public static void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp){
        try {
            sftp.cd(directory);
            File file = new File(saveFile);
            sftp.get(downloadFile, new FileOutputStream(file));
            System.out.println("下载成功");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 执行命令
     *
     * @param command 命令行
     * @param channelExec
     */
    public static void execCmd(String command,ChannelExec channelExec){
        try {
            InputStream in = channelExec.getInputStream();
            channelExec.setCommand(command);
            channelExec.setErrStream(System.err);
            channelExec.connect();
            String out = IOUtils.toString(in, "UTF-8");
            in.close();

            System.out.println(out);
//            return out;

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //关闭资源
    public static void closeResource(Session sshSession,ChannelSftp sftp){

        if(sshSession != null){
            sshSession.disconnect();
        }
        if(sftp != null){
            sftp.disconnect();
        }
    }

    public static void closeResource(Session sshSession,ChannelExec channelExec){

        if(sshSession != null){
            sshSession.disconnect();
        }
        if(channelExec != null){
            channelExec.disconnect();
        }
    }


}
最近下载更多
qiheideguang  LV18 2024年7月24日
438265764  LV14 2023年7月31日
15866685272  LV3 2023年5月4日
林间听风  LV10 2023年2月1日
lironggang  LV38 2022年11月20日
lxsnh123  LV3 2022年11月18日
skipple3  LV39 2022年10月18日
zw050256  LV7 2022年9月30日
wuyu8995861  LV7 2022年8月8日
liuyu-zui  LV4 2022年8月1日
最近浏览更多
qiheideguang  LV18 2024年7月24日
zhyoyu 2024年5月20日
暂无贡献等级
微信网友_6902352269217792  LV3 2024年3月12日
LARY  LV1 2024年1月5日
tangjianzhong 2023年12月19日
暂无贡献等级
kkkxyh  LV13 2023年12月11日
jiemomo  LV12 2023年10月19日
19050126312  LV1 2023年10月19日
2036495585  LV9 2023年9月25日
438265764  LV14 2023年7月31日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友