Ganymed SSH-2 for Java系列10之scpGet

Ganymed SSH-2 for Java系列10之scpGet


直接上代码:

  1. /** 
  2.      *  
  3.      * Get remote file through scp 
  4.      *  
  5.      * @param host 
  6.      *  
  7.      * @param username 
  8.      *  
  9.      * @param password 
  10.      *  
  11.      * @param remoteFile 
  12.      *  
  13.      * @param localDir 
  14.      *  
  15.      * @throws IOException 
  16.      */  
  17.   
  18.     public static void scpGet(String host, String username, String password,  
  19.   
  20.     String remoteFile, String localDir, int port) throws IOException {  
  21.   
  22.         if (logger.isInfoEnabled()) {  
  23.   
  24.             logger.info("spc [" + remoteFile + "] from " + host + " to "  
  25.                     + localDir);  
  26.   
  27.         }  
  28.   
  29.         Connection conn = null;  
  30.   
  31.         try {  
  32.             conn = getOpenedConnection(host, username, password, port);  
  33.   
  34.             SCPClient client = new SCPClient(conn);  
  35.   
  36.             client.get(remoteFile, localDir);  
  37.         } finally {  
  38.   
  39.             if (null != conn) {  
  40.   
  41.                 conn.close();  
  42.   
  43.             }  
  44.   
  45.         }  
  46.   
  47.     }  

测试:


  1. String remoteDir = "/usr/local/test.log";  
  2.     String localFile ="src/";  
  3.   
  4.     try {  
  5.   
  6.         CommandRunner.scpGet("172.16.18.141""root",  
  7.                 "123456", remoteDir,localFile, 22);  
  8.           
  9.     } catch (IOException e) {  
  10.         e.printStackTrace();  
  11.     }  


通过查看src目录下面,发现文件已经get下来了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值