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

      

  • 相关阅读:
    Spring+Quartz实现文件中转站
    Velocity中的ComparisonDateTool、MathTool、NumberT...
    Adobe Flash Builder 4.7破解方法(绝对可用)
    12c weblogic需要输入用户名密码
    velocity+spring mvc+spring ioc+ibatis初试感觉(与struts+spring+hibernate比较)
    基于tomcat7 web开发中的一点小东西
    Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.
    spring service事务传播
    Spring注解方式实现任务调度【官方文档翻译】
    Tomcat 下 Memcached 集群与 Terracotta 集群比较
  • 原文地址:https://www.cnblogs.com/haorun/p/6524734.html
Copyright © 2011-2022 走看看