Skip to content

Commit 5d30d0b

Browse files
authored
Merge pull request #17745 from Michaelin007/jschremote
https://jira.baeldung.com/browse/BAEL-8627
2 parents a14de25 + 48395d7 commit 5d30d0b

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

libraries-files/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@
4343
<version>${itext7-core.version}</version>
4444
<type>pom</type>
4545
</dependency>
46+
<dependency>
47+
<groupId>com.github.mwiede</groupId>
48+
<artifactId>jsch</artifactId>
49+
<version>${jsch.version}</version>
50+
</dependency>
4651
</dependencies>
4752

4853
<properties>
4954
<org.ini4j.version>0.5.4</org.ini4j.version>
5055
<commons-configuration2>2.8.0</commons-configuration2>
5156
<itext7-core.version>7.2.4</itext7-core.version>
57+
<jsch.version>0.2.20</jsch.version>
5258
</properties>
5359

5460
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.readfileremotely;
2+
3+
import com.jcraft.jsch.JSch;
4+
import com.jcraft.jsch.JSchException;
5+
import com.jcraft.jsch.Session;
6+
import com.jcraft.jsch.ChannelSftp;
7+
import com.jcraft.jsch.SftpException;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.BufferedReader;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.io.InputStreamReader;
15+
16+
public class RemoteServerJsch {
17+
18+
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteServerJsch.class);
19+
private static final String HOST = "HOST";
20+
private static final String USER = "USERNAME";
21+
private static final String PRIVATE_KEY = "PATH TO PRIVATE KEY";
22+
private static final int PORT = 22;
23+
24+
public static JSch setUpJsch() throws JSchException {
25+
JSch jsch = new JSch();
26+
jsch.addIdentity(PRIVATE_KEY);
27+
return jsch;
28+
}
29+
30+
public static Session createSession(JSch jsch) throws JSchException {
31+
Session session = jsch.getSession(USER, HOST, PORT);
32+
session.setConfig("StrictHostKeyChecking", "no");
33+
session.connect();
34+
return session;
35+
}
36+
37+
public static ChannelSftp createSftpChannel(Session session) throws JSchException {
38+
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
39+
channelSftp.connect();
40+
return channelSftp;
41+
}
42+
43+
public static void readFileLineByLine(ChannelSftp channelSftp, String filePath) throws SftpException, IOException {
44+
InputStream stream = channelSftp.get(filePath);
45+
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream))) {
46+
String line;
47+
while ((line = bufferedReader.readLine()) != null) {
48+
LOGGER.info(line);
49+
}
50+
}
51+
}
52+
53+
public static void disconnect(ChannelSftp channelSftp, Session session) {
54+
channelSftp.disconnect();
55+
session.disconnect();
56+
}
57+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.readfileremotely;
2+
3+
import com.jcraft.jsch.JSch;
4+
import com.jcraft.jsch.JSchException;
5+
import com.jcraft.jsch.Session;
6+
import com.jcraft.jsch.ChannelSftp;
7+
import com.jcraft.jsch.SftpException;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.IOException;
11+
12+
import static com.baeldung.readfileremotely.RemoteServerJsch.*;
13+
14+
class RemoteServerJschLiveTest {
15+
16+
private static final String REMOTE_DIR = "REMOTE_DIR";
17+
18+
@Test
19+
public void givenJsch_whenListFiles_thenSuccess() throws JSchException, SftpException, IOException {
20+
JSch jsch = setUpJsch();
21+
Session session = createSession(jsch);
22+
ChannelSftp channelSftp = createSftpChannel(session);
23+
readFileLineByLine(channelSftp, REMOTE_DIR + "/examplefile.txt");
24+
disconnect(channelSftp, session);
25+
}
26+
}

0 commit comments

Comments
 (0)