The main thing that caught my attention with FTP4CHE was the ability to connect to SSL/TLS enabled FTP servers using Java. Now the examples that comes with the package are, for most parts quite sufficient, though when you just need to know a few simple things, then it might be easier to just find examples that does just that. Keep in mind that the samples provided here are just excempts of the complete solution I made myself, but it should get you started. The comment field is open if you need extra information, and I’ll try my best to answer.
FTP4CHE supports the following connection types:
- Ordinary FTP connection (not encrypted)
- Implicit SSL
- Auth SSL
- Auth TLS
- Implicit TLS
Now on to the first example, how to connect to a SSL/TLS enabled FTP server.
Code
import org.ftp4che.*; import org.ftp4che.event.*; import org.ftp4che.proxy.*; import org.ftp4che.util.ftpfile.*; public class FTPTest implements FTPListener { private FTPConnection ftpConnection = null; public static void main(String[] args) { FTPTest ftp = new FTPTest(); ftp.connect(hostname, port, username, password, connectionType, timeout, passiveMode); } private FTPConnection connect(String hostname, int port, String username, String password, int connectionType, int timeout, boolean passiveMode) { FTPConnection connection = null; try { connection = FTPConnectionFactory.getInstance(getProperties(hostname, port, username, password, connectionType, timeout, passiveMode)); connection.addFTPStatusListener(this); connection.connect(); } catch (Exception e) {} ftpConnection = connection; return connection; } private Properties getProperties(String hostname, int port, String username, String password, int connectionType, int timeout, boolean passiveMode) { Properties pt = new Properties(); pt.setProperty("connection.host", hostname); pt.setProperty("connection.port", "" + port); pt.setProperty("user.login", username); pt.setProperty("user.password", password); pt.setProperty("connection.type", getConnectionType(connectionType)); pt.setProperty("connection.timeout", "" + timeout); pt.setProperty("connection.passive", "" + passiveMode); return pt; } private String getConnectionType(int connectionType) { String type = "FTP_CONNECTION"; if (connectionType == 2) { type = "IMPLICIT_SSL_FTP_CONNECTION"; } if (connectionType == 3) { type = "AUTH_SSL_FTP_CONNECTION"; } if (connectionType == 4) { type = "AUTH_TLS_FTP_CONNECTION"; } if (connectionType == 5) { type = "IMPLICIT_TLS_FTP_CONNECTION"; } return type; } }
|
If you found this publication useful to you, then feel free to show your appreaciation towards open-source publications by doing a small donation. It only takes a moment of your time. Any donation is appreaciated – and would also motivate me to release more as open-source. Donations so far – $0 USD – See previous donations. |






