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;
            }
    
        }
    
    }
    

      

  • 相关阅读:
    招聘测试开发二三事
    首次曝光:大厂都是这样过1024的,看的我酸了
    1024程序员节:今天,我们不加班!
    TesterHome创始人思寒:如何从手工测试进阶自动化测试?十余年经验分享
    ASP.NET网站中设置404自定义错误页面
    IIS 7 应用程序池自动回收关闭的解决方案
    ASP.NET项目中引用全局dll
    ASP.NET WebForm中前台代码如何绑定后台变量
    Git使用过程中出现项目文件无法签入Source Control的情况
    ASP.NET中身份验证的三种方法
  • 原文地址:https://www.cnblogs.com/haorun/p/6524734.html
Copyright © 2011-2022 走看看