Ganymed SSH-2 for Java系列11之SCPClient

14 篇文章 0 订阅

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都已经介绍完了,后面我将将这些知识整合起来,实现对远程服务器的一个部署操作,

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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值