zoukankan      html  css  js  c++  java
  • SCP 上传下载

    package test.com;
    
    import ch.ethz.ssh2.Connection;
    import ch.ethz.ssh2.SCPClient;
    
    /**
     * SCP 上传下载 用包 ganymed-ssh2-build210.jar
     */
    public class SCPFile {
    
        static Connection con = null;
    
        public static void main(String[] args) throws Exception {
    
            // 从服务器下载到本地,参数: IP,用户名,密码,远程路径,本地路径
            downLoadVideo("192.168.4.202", "gehr", "RNYyeTuqCE4NWWSqlH0A", "/home/gehr/nohup.out", "H:/");
    
            // 从本地上传到服务器,参数,IP,用户名,密码,远程路径,本地路径
            upLoadVideo("192.168.4.202", "gehr", "RNYyeTuqCE4NWWSqlH0A", "H:/zhazha.gif", "/home/gehr");
    
        }
    
        /**
         * 下载到本地
         * 
         * @throws Exception
         */
        public static boolean downLoadVideo(String IP, String user, String pass, String remotePath, String localPath) throws Exception {
    
            try {
                // 建立SCP客户端
                SCPClient scpClient = connectRemote(IP, user, pass);
                if (scpClient != null) {
                    // 服务器端的文件下载到本地的目录下
                    scpClient.get(remotePath, localPath);
                    return true;
                } else {
                    System.out.println("服务器连接失败");
                    return false;
                }
    
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
                return false;
            } finally {
                if (con != null) {
                    con.close();
                }
            }
    
        }
    
        /**
         * 上传到远程
         * 
         * @throws Exception
         */
        public static void upLoadVideo(String IP, String user, String pass, String remotePath, String localPath) throws Exception {
            // 建立SCP客户端
            SCPClient scpClient = connectRemote(IP, user, pass);
            if (scpClient != null) {
                // 文件上传到远程路径下
                scpClient.put(remotePath, localPath);
            }
    
            if (con != null) {
                con.close();
            }
    
        }
    
        /**
         * 连接远程服务器,返回true连接成功,返回false连接失败
         * 
         * @throws Exception
         */
        public static SCPClient connectRemote(String IP, String user, String pass) throws Exception {
            try {
                con = new Connection(IP);
                con.connect();
                // 远程服务器的用户名密码
                boolean isAuthed = con.authenticateWithPassword(user, pass);
                if (isAuthed) {
                    // 建立SCP客户端
                    SCPClient scpClient = con.createSCPClient();
                    return scpClient;
                } else {
                    System.out.println("连接远程服务器失败!");
                    System.out.println("连接远程服务器失败!");
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("服务器连接失败 IP:" + IP);
                return null;
            }
    
        }
    
    }
    

      

  • 相关阅读:
    第59节:Java中的html和css语言
    第59节:Java中的html和css语言
    小程序版本再次发布升级
    Java中的图形界面编程-GUI
    第58节:Java中的图形界面编程-GUI
    第58节:Java中的图形界面编程-GUI
    解决微信小程序登录与发布的一些问题
    解决微信小程序登录与发布的一些问题
    第57节:Java中流的操作以及编码解码
    第57节:Java中流的操作以及编码解码
  • 原文地址:https://www.cnblogs.com/haorun/p/6524734.html
Copyright © 2011-2022 走看看