Ftp
Introduction
The FTP client encapsulation is based on Apache Commons Net.
Usage
Adding Dependencies
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
Using the FTP Client
// Anonymous login (FTP server without username and password)
Ftp ftp = new Ftp("172.0.0.1");
// Enter remote directory
ftp.cd("/opt/upload");
// Upload local file
ftp.upload("/opt/upload", FileUtil.file("e:/test.jpg"));
// Download remote file
ftp.download("/opt/upload", "test.jpg", FileUtil.file("e:/test2.jpg"));
// Close the connection
ftp.close();
Active Mode and Passive Mode
- PORT (Active Mode)
The FTP client connects to the FTP server’s port 21, sends the username and password to log in. After a successful login, when it needs to list or read data, the client randomly opens a port (above 1024), sends the PORT command to the FTP server, telling the server that the client is using active mode and has opened a port. The FTP server receives the active mode command and port number, and connects to the client’s opened port through its port 20 to send data.
- PASV (Passive Mode)
The FTP client connects to the FTP server’s port 21, sends the username and password to log in. After a successful login, when it needs to list or read data, it sends the PASV command to the FTP server. The server randomly opens a port (above 1024) locally, and then tells the client the opened port number. The client then connects to the server’s opened port for data transmission.
For more information, see: https://www.cnblogs.com/huhaoshida/p/5412615.html
By default, Ftp uses passive mode. To switch to active mode:
Ftp ftp = new Ftp("172.0.0.1");
// Switch to active mode
ftp.setMode(FtpMode.Active);