您的位置:首页 > 编程语言 > Java开发

Ganymed SSH-2 for Java系列10之scpGet

2014-04-18 16:05 246 查看
Ganymed SSH-2 for Java系列10之scpGet

直接上代码:

[java]
view plaincopy





/** 
     *  
     * Get remote file through scp 
     *  
     * @param host 
     *  
     * @param username 
     *  
     * @param password 
     *  
     * @param remoteFile 
     *  
     * @param localDir 
     *  
     * @throws IOException 
     */  
  
    public static void scpGet(String host, String username, String password,  
  
    String remoteFile, String localDir, int port) throws IOException {  
  
        if (logger.isInfoEnabled()) {  
  
            logger.info("spc [" + remoteFile + "] from " + host + " to "  
                    + localDir);  
  
        }  
  
        Connection conn = null;  
  
        try {  
            conn = getOpenedConnection(host, username, password, port);  
  
            SCPClient client = new SCPClient(conn);  
  
            client.get(remoteFile, localDir);  
        } finally {  
  
            if (null != conn) {  
  
                conn.close();  
  
            }  
  
        }  
  
    }  

测试:

[java]
view plaincopy





String remoteDir = "/usr/local/test.log";  
    String localFile ="src/";  
  
    try {  
  
        CommandRunner.scpGet("172.16.18.141", "root",  
                "123456", remoteDir,localFile, 22);  
          
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

通过查看src目录下面,发现文件已经get下来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: