Kiview ftpclient (#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
This commit is contained in:
parent
272f018f90
commit
2b0c6ed655
|
@ -709,6 +709,20 @@
|
||||||
<artifactId>xchart</artifactId>
|
<artifactId>xchart</artifactId>
|
||||||
<version>${xchart-version}</version>
|
<version>${xchart-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-net</groupId>
|
||||||
|
<artifactId>commons-net</artifactId>
|
||||||
|
<version>${commons-net.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockftpserver</groupId>
|
||||||
|
<artifactId>MockFtpServer</artifactId>
|
||||||
|
<version>${mockftpserver.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
@ -923,6 +937,8 @@
|
||||||
<typesafe-akka.version>2.5.11</typesafe-akka.version>
|
<typesafe-akka.version>2.5.11</typesafe-akka.version>
|
||||||
<common-math3-version>3.6.1</common-math3-version>
|
<common-math3-version>3.6.1</common-math3-version>
|
||||||
<xchart-version>3.5.2</xchart-version>
|
<xchart-version>3.5.2</xchart-version>
|
||||||
|
<commons-net.version>3.6</commons-net.version>
|
||||||
|
<mockftpserver.version>2.7.1</mockftpserver.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.baeldung.ftp;
|
||||||
|
|
||||||
|
import org.apache.commons.net.PrintCommandListener;
|
||||||
|
import org.apache.commons.net.ftp.FTPClient;
|
||||||
|
import org.apache.commons.net.ftp.FTPFile;
|
||||||
|
import org.apache.commons.net.ftp.FTPReply;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
class FtpClient {
|
||||||
|
|
||||||
|
private final String server;
|
||||||
|
private final int port;
|
||||||
|
private final String user;
|
||||||
|
private final String password;
|
||||||
|
private FTPClient ftp;
|
||||||
|
|
||||||
|
FtpClient(String server, int port, String user, String password) {
|
||||||
|
this.server = server;
|
||||||
|
this.port = port;
|
||||||
|
this.user = user;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
void open() throws IOException {
|
||||||
|
ftp = new FTPClient();
|
||||||
|
|
||||||
|
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
|
||||||
|
|
||||||
|
ftp.connect(server, port);
|
||||||
|
int reply = ftp.getReplyCode();
|
||||||
|
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||||
|
ftp.disconnect();
|
||||||
|
throw new IOException("Exception in connecting to FTP Server");
|
||||||
|
}
|
||||||
|
|
||||||
|
ftp.login(user, password);
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() throws IOException {
|
||||||
|
ftp.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<String> listFiles(String path) throws IOException {
|
||||||
|
FTPFile[] files = ftp.listFiles(path);
|
||||||
|
|
||||||
|
return Arrays.stream(files)
|
||||||
|
.map(FTPFile::getName)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
void putFileToPath(File file, String path) throws IOException {
|
||||||
|
ftp.storeFile(path, new FileInputStream(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
void downloadFile(String source, String destination) throws IOException {
|
||||||
|
FileOutputStream out = new FileOutputStream(destination);
|
||||||
|
ftp.retrieveFile(source, out);
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ public class UserController {
|
||||||
public static Handler fetchById = ctx -> {
|
public static Handler fetchById = ctx -> {
|
||||||
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
|
int id = Integer.parseInt(Objects.requireNonNull(ctx.param("id")));
|
||||||
UserDao dao = UserDao.instance();
|
UserDao dao = UserDao.instance();
|
||||||
User user = dao.getUserById(id);
|
User user = dao.getUserById(id).get();
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
ctx.html("Not Found");
|
ctx.html("Not Found");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
package com.baeldung.ftp;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockftpserver.fake.FakeFtpServer;
|
||||||
|
import org.mockftpserver.fake.UserAccount;
|
||||||
|
import org.mockftpserver.fake.filesystem.DirectoryEntry;
|
||||||
|
import org.mockftpserver.fake.filesystem.FileEntry;
|
||||||
|
import org.mockftpserver.fake.filesystem.FileSystem;
|
||||||
|
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class FtpClientIntegrationTest {
|
||||||
|
|
||||||
|
private FakeFtpServer fakeFtpServer;
|
||||||
|
|
||||||
|
private FtpClient ftpClient;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws IOException {
|
||||||
|
fakeFtpServer = new FakeFtpServer();
|
||||||
|
fakeFtpServer.addUserAccount(new UserAccount("user", "password", "/data"));
|
||||||
|
|
||||||
|
FileSystem fileSystem = new UnixFakeFileSystem();
|
||||||
|
fileSystem.add(new DirectoryEntry("/data"));
|
||||||
|
fileSystem.add(new FileEntry("/data/foobar.txt", "abcdef 1234567890"));
|
||||||
|
fakeFtpServer.setFileSystem(fileSystem);
|
||||||
|
fakeFtpServer.setServerControlPort(0);
|
||||||
|
|
||||||
|
fakeFtpServer.start();
|
||||||
|
|
||||||
|
ftpClient = new FtpClient("localhost", fakeFtpServer.getServerControlPort(), "user", "password");
|
||||||
|
ftpClient.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() throws IOException {
|
||||||
|
ftpClient.close();
|
||||||
|
fakeFtpServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRemoteFile_whenListingRemoteFiles_thenItIsContainedInList() throws IOException {
|
||||||
|
Collection<String> files = ftpClient.listFiles("");
|
||||||
|
|
||||||
|
assertThat(files).contains("foobar.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
|
||||||
|
ftpClient.downloadFile("/foobar.txt", "downloaded_buz.txt");
|
||||||
|
|
||||||
|
assertThat(new File("downloaded_buz.txt")).exists();
|
||||||
|
new File("downloaded_buz.txt").delete(); // cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLocalFile_whenUploadingIt_thenItExistsOnRemoteLocation() throws URISyntaxException, IOException {
|
||||||
|
File file = new File(getClass().getClassLoader().getResource("ftp/baz.txt").toURI());
|
||||||
|
|
||||||
|
ftpClient.putFileToPath(file, "/buz.txt");
|
||||||
|
|
||||||
|
assertThat(fakeFtpServer.getFileSystem().exists("/buz.txt")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
package com.baeldung.ftp;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockftpserver.fake.FakeFtpServer;
|
||||||
|
import org.mockftpserver.fake.UserAccount;
|
||||||
|
import org.mockftpserver.fake.filesystem.DirectoryEntry;
|
||||||
|
import org.mockftpserver.fake.filesystem.FileEntry;
|
||||||
|
import org.mockftpserver.fake.filesystem.FileSystem;
|
||||||
|
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class JdkFtpClientIntegrationTest {
|
||||||
|
|
||||||
|
private FakeFtpServer fakeFtpServer;
|
||||||
|
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() throws IOException {
|
||||||
|
fakeFtpServer = new FakeFtpServer();
|
||||||
|
fakeFtpServer.addUserAccount(new UserAccount("user", "password", "/data"));
|
||||||
|
|
||||||
|
FileSystem fileSystem = new UnixFakeFileSystem();
|
||||||
|
fileSystem.add(new DirectoryEntry("/data"));
|
||||||
|
fileSystem.add(new FileEntry("/data/foobar.txt", "abcdef 1234567890"));
|
||||||
|
fakeFtpServer.setFileSystem(fileSystem);
|
||||||
|
fakeFtpServer.setServerControlPort(0);
|
||||||
|
|
||||||
|
fakeFtpServer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() throws IOException {
|
||||||
|
fakeFtpServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
|
||||||
|
String ftpUrl = String.format("ftp://user:password@localhost:%d/foobar.txt", fakeFtpServer.getServerControlPort());
|
||||||
|
|
||||||
|
URLConnection urlConnection = new URL(ftpUrl).openConnection();
|
||||||
|
InputStream inputStream = urlConnection.getInputStream();
|
||||||
|
Files.copy(inputStream, new File("downloaded_buz.txt").toPath());
|
||||||
|
inputStream.close();
|
||||||
|
|
||||||
|
assertThat(new File("downloaded_buz.txt")).exists();
|
||||||
|
|
||||||
|
new File("downloaded_buz.txt").delete(); // cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,3 +1,3 @@
|
||||||
### Relevant articles
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Deploy a Spring Boot WAR into a Tomcat Server](http://www.baeldung.com/spring-boot-war-tomcat-deploy)
|
- [Deploy a Spring Boot WAR into a Tomcat Server](http://www.baeldung.com/spring-boot-war-tomcat-deploy)
|
||||||
|
|
Loading…
Reference in New Issue