Skip to content

Commit 7c48f20

Browse files
committed
https://jira.baeldung.com/browse/BAEL-8627
1 parent 375972d commit 7c48f20

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
17+
public class RemoteServerJsch {
18+
19+
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteServerJsch.class);
20+
private static final String HOST = "HOST";
21+
private static final String USER = "USERNAME";
22+
private static final String PRIVATE_KEY = "PATH TO PRIVATE KEY";
23+
private static final int PORT = 22;
24+
25+
26+
public static JSch setUpJsch() throws JSchException {
27+
JSch jsch = new JSch();
28+
jsch.addIdentity(PRIVATE_KEY);
29+
return jsch;
30+
}
31+
32+
public static Session createSession(JSch jsch) throws JSchException {
33+
Session session = jsch.getSession(USER, HOST, PORT);
34+
session.setConfig("StrictHostKeyChecking", "no");
35+
session.connect();
36+
return session;
37+
}
38+
39+
public static ChannelSftp createSftpChannel(Session session) throws JSchException {
40+
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
41+
channelSftp.connect();
42+
return channelSftp;
43+
}
44+
45+
public static void readFileLineByLine(ChannelSftp channelSftp, String filePath) throws SftpException, IOException {
46+
InputStream stream = channelSftp.get(filePath);
47+
try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
48+
String line;
49+
while ((line = br.readLine()) != null) {
50+
LOGGER.info(line);
51+
}
52+
}
53+
}
54+
55+
public static void disconnect(ChannelSftp channelSftp, Session session) {
56+
channelSftp.disconnect();
57+
session.disconnect();
58+
}
59+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
27+
}

0 commit comments

Comments
 (0)