scp java_scp ssh java 实例

pom

ch.ethz.ganymed

ganymed-ssh2

build210

package com.taskschedule.util;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import ch.ethz.ssh2.Connection;

import ch.ethz.ssh2.SCPClient;

import ch.ethz.ssh2.SFTPException;

import ch.ethz.ssh2.SFTPv3Client;

import ch.ethz.ssh2.Session;

import ch.ethz.ssh2.StreamGobbler;

/**

*

* @author baoyou E-mail:curiousby@163.com

* @version 2016年11月2日 下午1:54:43

* desc:

*/

public class Scp {

private static Logger logger = Logger.getLogger(Scp.class);

/**

* 远程文件传输,如果local参数是文件,则本地传输到远程;如果是目录,则远程传输到本地

* @param remoteIp 远程主机IP或hostname

* @param port 远程主机端口号

* @param user 远程主机用户名

* @param password 远程主机对应密码

* @param local 本地主机文件名(本地->远程);本地主机目录名(远程->本地)

* @param remote 远程主机目录名(本地->远程);远程主机文件名(远程->本地)

* @return 返回0:成功。1:失败

*/

public static int scpFile(String remoteIp, int port, String user, String password, String local,

String remote) {

Connection con = new Connection(remoteIp, port);

try {

con.connect();

boolean isAuthed = con.authenticateWithPassword(user, password);

if (!isAuthed) {

logger.error("远程主机" + remoteIp + "用户名或密码验证失败!");

return SystemGlobal.FAILED;

}

SCPClient scpClient = con.createSCPClient();

File localFile = new File(local);

if (localFile.isFile()) {

if (!localFile.exists()) {

logger.error("本地文件" + local + "不存在,无法传输!");

return SystemGlobal.FAILED;

} else {

try {

SFTPv3Client sftpClient = new SFTPv3Client(con);

sftpClient.mkdir(remote, 0777); //远程新建目录

} catch (SFTPException e1) {

logger.info("目录" + remote + "已存在,无需再创建。");

}

try {

scpClient.put(local, remote, "0777");

} catch (IOException e2) {

logger.error("路径" + remote + "不是一个文件夹。");

return SystemGlobal.FAILED;

}

String filename = local.substring(local.lastIndexOf('/') + 1);

Session session = con.openSession();

session.execCommand("ls -l " + remote + "/" + filename);

InputStream is = new StreamGobbler(session.getStdout());

BufferedReader br = new BufferedReader(new InputStreamReader(is));

String line = br.readLine();

br.close();

is.close();

session.close();

logger.info("上传到远程主机上的文件为:" + line);

if (!TaskUtil.isStrEmpty(line)) {

return SystemGlobal.SUCCESS;

}

}

} else {

if (!localFile.exists()) {

localFile.mkdirs();

}

scpClient.get(remote, local);

String filename = remote.substring(remote.lastIndexOf('/') + 1);

if (new File(local + "/" + filename).exists()) {

return SystemGlobal.SUCCESS;

}

}

} catch (Exception e) {

e.printStackTrace();

}

con.close();

return SystemGlobal.FAILED;

}

}

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。

谢谢您的赞助,我会做的更好!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值