Skip to content

Commit 2b0c6ed

Browse files
pivovaritmaibin
authored andcommitted
Kiview ftpclient (eugenp#4456)
* Add example for FTP client Using Apache Commons Net * Add missing resource * Fix wrong file download in integration test * Add example for using FTP support in JDK * Close input stream after copy * Fix test name
1 parent 272f018 commit 2b0c6ed

7 files changed

Lines changed: 217 additions & 2 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,20 @@
709709
<artifactId>xchart</artifactId>
710710
<version>${xchart-version}</version>
711711
</dependency>
712+
713+
<dependency>
714+
<groupId>commons-net</groupId>
715+
<artifactId>commons-net</artifactId>
716+
<version>${commons-net.version}</version>
717+
</dependency>
718+
<dependency>
719+
<groupId>org.mockftpserver</groupId>
720+
<artifactId>MockFtpServer</artifactId>
721+
<version>${mockftpserver.version}</version>
722+
<scope>test</scope>
723+
</dependency>
724+
725+
712726
</dependencies>
713727

714728
<repositories>
@@ -923,6 +937,8 @@
923937
<typesafe-akka.version>2.5.11</typesafe-akka.version>
924938
<common-math3-version>3.6.1</common-math3-version>
925939
<xchart-version>3.5.2</xchart-version>
940+
<commons-net.version>3.6</commons-net.version>
941+
<mockftpserver.version>2.7.1</mockftpserver.version>
926942
</properties>
927943

928944
</project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.ftp;
2+
3+
import org.apache.commons.net.PrintCommandListener;
4+
import org.apache.commons.net.ftp.FTPClient;
5+
import org.apache.commons.net.ftp.FTPFile;
6+
import org.apache.commons.net.ftp.FTPReply;
7+
8+
import java.io.*;
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
import java.util.stream.Collectors;
12+
13+
class FtpClient {
14+
15+
private final String server;
16+
private final int port;
17+
private final String user;
18+
private final String password;
19+
private FTPClient ftp;
20+
21+
FtpClient(String server, int port, String user, String password) {
22+
this.server = server;
23+
this.port = port;
24+
this.user = user;
25+
this.password = password;
26+
}
27+
28+
void open() throws IOException {
29+
ftp = new FTPClient();
30+
31+
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
32+
33+
ftp.connect(server, port);
34+
int reply = ftp.getReplyCode();
35+
if (!FTPReply.isPositiveCompletion(reply)) {
36+
ftp.disconnect();
37+
throw new IOException("Exception in connecting to FTP Server");
38+
}
39+
40+
ftp.login(user, password);
41+
}
42+
43+
void close() throws IOException {
44+
ftp.disconnect();
45+
}
46+
47+
Collection<String> listFiles(String path) throws IOException {
48+
FTPFile[] files = ftp.listFiles(path);
49+
50+
return Arrays.stream(files)
51+
.map(FTPFile::getName)
52+
.collect(Collectors.toList());
53+
}
54+
55+
void putFileToPath(File file, String path) throws IOException {
56+
ftp.storeFile(path, new FileInputStream(file));
57+
}
58+
59+
void downloadFile(String source, String destination) throws IOException {
60+
FileOutputStream out = new FileOutputStream(destination);
61+
ftp.retrieveFile(source, out);
62+
}
63+
}

‎libraries/src/main/java/com/baeldung/javalin/User/UserController.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class UserController {
1414
public static Handler fetchById = ctx -> {
1515
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
1616
UserDao dao = UserDao.instance();
17-
User user = dao.getUserById(id);
17+
User user = dao.getUserById(id).get();
1818
if (user == null) {
1919
ctx.html("Not Found");
2020
} else {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.ftp;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockftpserver.fake.FakeFtpServer;
7+
import org.mockftpserver.fake.UserAccount;
8+
import org.mockftpserver.fake.filesystem.DirectoryEntry;
9+
import org.mockftpserver.fake.filesystem.FileEntry;
10+
import org.mockftpserver.fake.filesystem.FileSystem;
11+
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.net.URISyntaxException;
16+
import java.util.Collection;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
public class FtpClientIntegrationTest {
21+
22+
private FakeFtpServer fakeFtpServer;
23+
24+
private FtpClient ftpClient;
25+
26+
@Before
27+
public void setup() throws IOException {
28+
fakeFtpServer = new FakeFtpServer();
29+
fakeFtpServer.addUserAccount(new UserAccount("user", "password", "/data"));
30+
31+
FileSystem fileSystem = new UnixFakeFileSystem();
32+
fileSystem.add(new DirectoryEntry("/data"));
33+
fileSystem.add(new FileEntry("/data/foobar.txt", "abcdef 1234567890"));
34+
fakeFtpServer.setFileSystem(fileSystem);
35+
fakeFtpServer.setServerControlPort(0);
36+
37+
fakeFtpServer.start();
38+
39+
ftpClient = new FtpClient("localhost", fakeFtpServer.getServerControlPort(), "user", "password");
40+
ftpClient.open();
41+
}
42+
43+
@After
44+
public void teardown() throws IOException {
45+
ftpClient.close();
46+
fakeFtpServer.stop();
47+
}
48+
49+
@Test
50+
public void givenRemoteFile_whenListingRemoteFiles_thenItIsContainedInList() throws IOException {
51+
Collection<String> files = ftpClient.listFiles("");
52+
53+
assertThat(files).contains("foobar.txt");
54+
}
55+
56+
@Test
57+
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
58+
ftpClient.downloadFile("/foobar.txt", "downloaded_buz.txt");
59+
60+
assertThat(new File("downloaded_buz.txt")).exists();
61+
new File("downloaded_buz.txt").delete(); // cleanup
62+
}
63+
64+
@Test
65+
public void givenLocalFile_whenUploadingIt_thenItExistsOnRemoteLocation() throws URISyntaxException, IOException {
66+
File file = new File(getClass().getClassLoader().getResource("ftp/baz.txt").toURI());
67+
68+
ftpClient.putFileToPath(file, "/buz.txt");
69+
70+
assertThat(fakeFtpServer.getFileSystem().exists("/buz.txt")).isTrue();
71+
}
72+
73+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.ftp;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.mockftpserver.fake.FakeFtpServer;
7+
import org.mockftpserver.fake.UserAccount;
8+
import org.mockftpserver.fake.filesystem.DirectoryEntry;
9+
import org.mockftpserver.fake.filesystem.FileEntry;
10+
import org.mockftpserver.fake.filesystem.FileSystem;
11+
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
12+
13+
import java.io.File;
14+
import java.io.FileOutputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.net.URISyntaxException;
18+
import java.net.URL;
19+
import java.net.URLConnection;
20+
import java.nio.file.Files;
21+
import java.util.Collection;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
public class JdkFtpClientIntegrationTest {
26+
27+
private FakeFtpServer fakeFtpServer;
28+
29+
30+
@Before
31+
public void setup() throws IOException {
32+
fakeFtpServer = new FakeFtpServer();
33+
fakeFtpServer.addUserAccount(new UserAccount("user", "password", "/data"));
34+
35+
FileSystem fileSystem = new UnixFakeFileSystem();
36+
fileSystem.add(new DirectoryEntry("/data"));
37+
fileSystem.add(new FileEntry("/data/foobar.txt", "abcdef 1234567890"));
38+
fakeFtpServer.setFileSystem(fileSystem);
39+
fakeFtpServer.setServerControlPort(0);
40+
41+
fakeFtpServer.start();
42+
}
43+
44+
@After
45+
public void teardown() throws IOException {
46+
fakeFtpServer.stop();
47+
}
48+
49+
@Test
50+
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
51+
String ftpUrl = String.format("ftp://user:password@localhost:%d/foobar.txt", fakeFtpServer.getServerControlPort());
52+
53+
URLConnection urlConnection = new URL(ftpUrl).openConnection();
54+
InputStream inputStream = urlConnection.getInputStream();
55+
Files.copy(inputStream, new File("downloaded_buz.txt").toPath());
56+
inputStream.close();
57+
58+
assertThat(new File("downloaded_buz.txt")).exists();
59+
60+
new File("downloaded_buz.txt").delete(); // cleanup
61+
}
62+
63+
}

‎libraries/src/test/resources/ftp/baz.txt‎

Whitespace-only changes.

‎spring-boot-ops/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
### Relevant articles
1+
### Relevant Articles:
22

33
- [Deploy a Spring Boot WAR into a Tomcat Server](http://www.baeldung.com/spring-boot-war-tomcat-deploy)

0 commit comments

Comments
 (0)