Ganymed SSH-2 for Java系列11之SCPClient

Ganymed SSH-2 for Java系列11之SCPClient


SCPClient是一个基本的java操作类,其可以从服务器复制文件到SSH-2服务器,或者从服务器上scp出文件到本地服务器;

其操作的scp路径必须是存在与服务器上的,否则会报错。


public void put(String localFile, String remoteTargetDirectory) throws IOException

--》本地文件复制到远程目录,创建时使用的模式0600 即rw

  1. public void put(String localFile, String remoteTargetDirectory) throws IOException  
  2. {  
  3.     put(new String[] { localFile }, remoteTargetDirectory, "0600");  
  4. }  

public void put(String[] localFiles, String remoteTargetDirectory) throws IOException

--》java多态,复制一组本地文件到远程目录,默认使用0600权限码;

  1. public void put(String[] localFiles, String remoteTargetDirectory) throws IOException  
  2. {  
  3.     put(localFiles, remoteTargetDirectory, "0600");  
  4. }  

public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException

--》java多态,本地文件复制到远程目录,使用指定的模式创建远程端上的文件。

  1. public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException  
  2.     {  
  3.         put(new String[] { localFile }, remoteTargetDirectory, mode);  
  4.     }  

public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode)
throws IOException

--》java多态,本地文件复制到远程目录,使用指定的模式和远程文件名创建远程端上的文件。

  1. public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode)  
  2.             throws IOException  
  3.     {  
  4.         put(new String[] { localFile }, new String[] { remoteFileName }, remoteTargetDirectory, mode);  
  5.     }  


public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException

--》java多态,创建一个远程文件将传递的字节数组的内容复制到其中,默认模式0600创建远程文件。

  1. public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException  
  2.     {  
  3.         put(data, remoteFileName, remoteTargetDirectory, "0600");  
  4.     }  

其余的put方法请参考源码;

public void get(String remoteFile, String localTargetDirectory) throws IOException

--》从远程服务器上下载文件到本地目录;

  1. public void get(String remoteFile, String localTargetDirectory) throws IOException  
  2.     {  
  3.         get(new String[] { remoteFile }, localTargetDirectory);  
  4.     }  

public void get(String remoteFiles[], String localTargetDirectory) throws IOException

--》java多态,从远程服务器下载一组文件到本地目录。

  1. public void get(String remoteFiles[], String localTargetDirectory) throws IOException  
  2.     {  
  3.         Session sess = null;  
  4.   
  5.         if ((remoteFiles == null) || (localTargetDirectory == null))  
  6.             throw new IllegalArgumentException("Null argument.");  
  7.   
  8.         if (remoteFiles.length == 0)  
  9.             return;  
  10.   
  11.         String cmd = "scp -f";  
  12.   
  13.         for (int i = 0; i < remoteFiles.length; i++)  
  14.         {  
  15.             if (remoteFiles[i] == null)  
  16.                 throw new IllegalArgumentException("Cannot accept null filename.");  
  17.   
  18.             String tmp = remoteFiles[i].trim();  
  19.   
  20.             if (tmp.length() == 0)  
  21.                 throw new IllegalArgumentException("Cannot accept empty filename.");  
  22.   
  23.             cmd += (" " + tmp);  
  24.         }  
  25.   
  26.         try  
  27.         {  
  28.             sess = conn.openSession();  
  29.             sess.execCommand(cmd);  
  30.             receiveFiles(sess, remoteFiles, localTargetDirectory);  
  31.         }  
  32.         catch (IOException e)  
  33.         {  
  34.             throw (IOException) new IOException("Error during SCP transfer.").initCause(e);  
  35.         }  
  36.         finally  
  37.         {  
  38.             if (sess != null)  
  39.                 sess.close();  
  40.         }  
  41.     }  


到此为止,ssh 2 for java的一些核心的api都已经介绍完了,后面我将将这些知识整合起来,实现对远程服务器的一个部署操作,

这个作为一个实例操作,希望自己后面有时间完成这个功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值