Java之操作SCP

Java操作SCP的场景很多,如下:

  • 1.从B服务器拉取数据文件,读取并存储到数据库或其它存储介质;
  • 2.特定时间将数据文件从A服务器上传到C服务器,便于C服务器特定运算数据文件并存储到数据库或其它存储介质。

一、导入Maven依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-scp</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>

二、编写核心方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* 从本地上传至服务器
*
* @param userName
* @param password
* @param ipAddr
* @param loadFilePath
* @param serverPath
* @return
*/
public static boolean localToServer(String userName, String password, String ipAddr, String loadFilePath, String serverPath) {
boolean isAuthed = false;
try {
if (InetAddress.getByName(ipAddr).isReachable(1500)) {
Connection conn = new Connection(ipAddr);
conn.connect();
isAuthed = conn.authenticateWithPassword(userName, password);
if (isAuthed) {
SCPClient scpClient = conn.createSCPClient();
scpClient.put(loadFilePath, serverPath);
conn.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}

return isAuthed;
}

/**
* 从服务器拉取文件到本地
*
* @param userName
* @param password
* @param ipAddr
* @param serverPath
* @param localPath
* @return
*/
public static boolean serverToLocal(String userName, String password, String ipAddr, String serverPath, String localPath) {
boolean isAuthed = false;
boolean status = false;
try {
status = InetAddress.getByName(ipAddr).isReachable(1500);
System.out.println(status);
if (status) {
Connection conn = new Connection(ipAddr);
conn.connect();
isAuthed = conn.authenticateWithPassword(userName, password);
System.out.println(isAuthed);
if (isAuthed) {
Session session = conn.openSession();
SCPClient scpClient = conn.createSCPClient();
scpClient.get(serverPath, localPath);
session.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}

return isAuthed;
}

三、简单测试

1
2
3
4
public static void main(String[] args) {
// localToServer("tech","zxcvb","192.168.10.2","D://Tools//test.txt","/home/tech/test/");
serverToLocal("tech", "zxcvb", "192.168.10.2", "/home/tech/test/***", "D://Tools//test//");
}
文章目录
  1. 一、导入Maven依赖
  2. 二、编写核心方法
  3. 三、简单测试