Overview
This article will demonstrate how to securely transfer files using the FTPS (FTP over SSL) protocol and the components provided in Secure FTP Factory or Secure iNet Factory. To see what else these components has to offer please download a FREE 30 day evaluation here, Secure FTP Factory Evaluation and Secure iNet Factory Evaluation.
Note: This example demonstrates transferring files using FTPS (FTP over SSL). If you want to learn how to use SFTP (FTP over SSH) protocol to transfer files then please see the article Secure FTP using Java and SFTP (FTP over SSH).
Prerequisites
- An FTP server with SSL support such as JSCAPE MFT Server
- JDK 1.4 or above
- Secure FTP Factory or Secure iNet Factory
Downloading Files From an FTPS Server using Ftps Class
The Ftps class provides an easy to use programming interface for securely exchanging data with an FTP (File Transfer Protocol) server using SSL (Secure Sockets Layer)
There are two ways to define a class that use Ftps class. If you want to listen to events fired by the underlying FTP instance you could have your class implement the FtpListener interface or extend the FtpAdapater class, which already implements FtpListener and provide a default implementation.
//import the necessary class
import com.jscape.inet.ftp.FtpAdapter;
public class FtpsDownloadTutorial extends FtpAdapter {
…
}
Or if not interested on listening to FTP events then a plain Java class will do.
public class FtpsDownloadTutorial {
…
}
Below is a code snippet that uses Ftps class to connect an FTP server with SSL support and download files matching a given regex filter.
//import the necessary classes
import com.jscape.inet.ftp.FtpException;
import com.jscape.inet.ftps.Ftps;
…
// perform multiple file download
public void doDownload(String hostname, String username, String password, String filter)
throws FtpException
{
Ftps ftp = new Ftps(hostname, username, password);
ftp.setDebug(true);
ftp.setTimeout(1000);
//capture Ftp related events if applicable (if your class extends FtpAdapter)
ftp.addFtpListener(this);
//establish connection to the server
ftp.connect();
//set file transfer mode to BINARY. if transferring text it's best to use setAscii()
ftp.setBinary();
//download files that matched the given regex filter
ftp.mdownload(filter);
//disconnect from server
ftp.disconnect();
}
…
Download the complete Java code for this tutorial here: FtpsDownloadTutorial.java
Summary
Being able to transfer files securely is sometimes a critical requirement to some applications. Using Secure FTP Factory or Secure iNet Factory you can quickly and easily enable such functionality within your applications.
References
Comments