Ganymed SSH-2 for Java系列9之scpPut

14 篇文章 0 订阅

 Ganymed SSH-2 for Java系列8之scpPut


/**
	 * 
	 * Put local file to remote machine.
	 * 
	 * @param host
	 * 
	 * @param username
	 * 
	 * @param password
	 * 
	 * @param localFile
	 * 
	 * @param remoteDir
	 * 
	 * @throws IOException
	 */

	public static void scpPut(String host, String username, String password,

	String localFile, String remoteDir, String permissions_code, int port)
			throws IOException {

		if (logger.isInfoEnabled()) {

			logger.info("scp [" + localFile + "] to " + host + remoteDir);

		}

		Connection conn = null;

		try {
			conn = getOpenedConnection(host, username, password, port);

			SCPClient client = new SCPClient(conn);

			client.put(localFile, remoteDir, permissions_code);
		} finally {
			if (null != conn) {
				conn.close();
			}
		}

	}

各个参数说明:

String host 服务器ip

String username 用户名

 String password, 密码

String localFile,本地文件

 String remoteDir, 远程目录

 String permissions_code,权限码

 int port ssh端口


示例:

	String remoteDir = "/usr/local";
		String localFile ="src/test.log";

		try {

			CommandRunner.scpPut("172.16.18.141", "root",
					"123456", localFile, remoteDir,"0060", 22);
			
		} catch (IOException e) {
			e.printStackTrace();
		}

说明:

这个操作是将本地的test.log上传到远程目录:usr local目录下面,如果重复上传同一个文件,会覆盖掉上一个文件,

如果第一次赋予的 权限码是0060 ,对同一个文件上传,第二次赋予的权限码是0600,那么其权限不会被改变,

但是文件会被第二次上传的文件覆盖掉。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值