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

public void put(String localFile, String remoteTargetDirectory) throws IOException{put(new String[] { localFile }, remoteTargetDirectory, "0600");}public void put(String[] localFiles, String remoteTargetDirectory) throws IOException

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

public void put(String[] localFiles, String remoteTargetDirectory) throws IOException{put(localFiles, remoteTargetDirectory, "0600");}public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException

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

public void put(String localFile, String remoteTargetDirectory, String mode) throws IOException{put(new String[] { localFile }, remoteTargetDirectory, mode);}public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode)throws IOException

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

public void put(String localFile, String remoteFileName, String remoteTargetDirectory, String mode)throws IOException{put(new String[] { localFile }, new String[] { remoteFileName }, remoteTargetDirectory, mode);}

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

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

public void put(byte[] data, String remoteFileName, String remoteTargetDirectory) throws IOException{put(data, remoteFileName, remoteTargetDirectory, "0600");}其余的put方法请参考源码;

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

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

public void get(String remoteFile, String localTargetDirectory) throws IOException{get(new String[] { remoteFile }, localTargetDirectory);}public void get(String remoteFiles[], String localTargetDirectory) throws IOException

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

public void get(String remoteFiles[], String localTargetDirectory) throws IOException{Session sess = null;if ((remoteFiles == null) || (localTargetDirectory == null))throw new IllegalArgumentException("Null argument.");if (remoteFiles.length == 0)return;String cmd = "scp -f";for (int i = 0; i < remoteFiles.length; i++){if (remoteFiles[i] == null)throw new IllegalArgumentException("Cannot accept null filename.");String tmp = remoteFiles[i].trim();if (tmp.length() == 0)throw new IllegalArgumentException("Cannot accept empty filename.");cmd += (" " + tmp);}try{sess = conn.openSession();sess.execCommand(cmd);receiveFiles(sess, remoteFiles, localTargetDirectory);}catch (IOException e){throw (IOException) new IOException("Error during SCP transfer.").initCause(e);}finally{if (sess != null)sess.close();}}

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

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

自信的生命最美丽!

2 for Java系列11之SCPClient

相关文章:

你感兴趣的文章:

标签云: