您的位置:首页 > 编程语言 > Java开发

java调用scp,文件从本地复制到远程

2017-06-20 19:39 351 查看
//数据服务器的ip地址
private String dataServerIp = "192.168.123.123";
//数据服务器的用户名
private String dataServerUsername = "root";
//数据服务器的密码
private String dataServerPassword = "1234";
//数据服务器的目的文件夹
private String dataServerDestDir = "/export/App/uploadDir/";
//从远程到本地的保存路径
//    private String localDir = "D:\\上传文件的临时目录";

//文件scp到数据服务器
Connection conn = new Connection(dataServerIp);
log.info("开始scp文件");
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(dataServerUsername, dataServerPassword);
if (isAuthenticated == false)
throw new IOException("Authentication failed.文件scp到数据服务器时发生异常");
SCPClient client = new SCPClient(conn);
client.put("D:\\00审计.zip", dataServerDestDir); //本地文件scp到远程目录
//            client.get(dataServerDestDir + "00审计.zip", localDir);//远程的文件scp到本地目录
conn.close();
} catch (IOException e) {
e.printStackTrace();
log.error("文件scp到数据服务器时发生异常");
}
log.info("scp文件结束");

调用的jar

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>


以上亲测可用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java linux scp 复制 远程