-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFTPClient
101 lines (80 loc) · 3.68 KB
/
SFTPClient
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.sftp.client.SftpClient;
import org.apache.sshd.sftp.client.SftpClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class SftpClientWithKeyAuth {
private static final Logger logger = LoggerFactory.getLogger(SftpClientWithKeyAuth.class);
private static final String HOST = "localhost";
private static final int PORT = 2222;
private static final String USERNAME = "your_username";
private static final String PRIVATE_KEY_PATH = "path/to/your/private_key";
private final SshClient client;
public SftpClientWithKeyAuth() {
this.client = SshClient.setUpDefaultClient();
}
public void connect() throws IOException {
client.start();
ClientSession session = client.connect(USERNAME, HOST, PORT)
.verify(10000)
.getSession();
session.addPublicKeyIdentity(Paths.get(PRIVATE_KEY_PATH));
session.auth().verify(30000);
logger.info("Successfully connected to SFTP server");
}
public void uploadFile(String localFilePath, String remoteFilePath) throws IOException {
try (ClientSession session = client.connect(USERNAME, HOST, PORT).verify(10000).getSession()) {
session.addPublicKeyIdentity(Paths.get(PRIVATE_KEY_PATH));
session.auth().verify(30000);
SftpClient sftpClient = SftpClientFactory.instance().createSftpClient(session);
try (InputStream inputStream = Files.newInputStream(Paths.get(localFilePath));
OutputStream outputStream = sftpClient.write(remoteFilePath)) {
byte[] buffer = new byte[8192];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
}
logger.info("File uploaded successfully: {}", remoteFilePath);
}
}
public void downloadFile(String remoteFilePath, String localFilePath) throws IOException {
try (ClientSession session = client.connect(USERNAME, HOST, PORT).verify(10000).getSession()) {
session.addPublicKeyIdentity(Paths.get(PRIVATE_KEY_PATH));
session.auth().verify(30000);
SftpClient sftpClient = SftpClientFactory.instance().createSftpClient(session);
try (InputStream inputStream = sftpClient.read(remoteFilePath);
OutputStream outputStream = Files.newOutputStream(Paths.get(localFilePath))) {
byte[] buffer = new byte[8192];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
}
logger.info("File downloaded successfully: {}", localFilePath);
}
}
public void disconnect() throws IOException {
client.stop();
logger.info("Disconnected from SFTP server");
}
public static void main(String[] args) {
SftpClientWithKeyAuth client = new SftpClientWithKeyAuth();
try {
client.connect();
// Example usage
client.uploadFile("path/to/local/file.txt", "/remote/path/file.txt");
client.downloadFile("/remote/path/file.txt", "path/to/local/downloaded_file.txt");
client.disconnect();
} catch (IOException e) {
logger.error("Error occurred during SFTP operations", e);
}
}
}