Java FTP
Overview
The ability to transfer files from one machine to another is often
accomplished using an FTP client. Using the Java FTP component provided
in Secure FTP Factory this article will demonstrate how you can easily
embed FTP capabilities into your own Java applications.
In addition to support for the FTP protocol, Secure FTP Factory also has components for secure file transfer protocols including FTPS (FTP over SSL both implicit and explicit modes), SFTP (FTP over SSH) and SCP (Secure Copy via SSH).
 
Secure FTP Factory
JSCAPE Secure FTP Server
Secure FTP Applet
The FTP protocol is in wide use for transferring files from one
computer to another. The FTP protocol is published under RFC 959.
Details of this protocol are beyond the scope of this article
however if you are interested in learning more on the details
of the FTP protocol go to http://www.faqs.org/rfcs/rfc959.html
Importing the necessary components
In order to use FTP you must ensure that the following import
statements are included in your Java code. The com.jscape.inet.ftp package contains the necessary classes for communicating with
an FTP server.
import com.jscape.inet.ftp.*;
Establishing a connection
In order to communicate with an FTP server you must first establish
a network connection to the FTP server.
// create Ftp instance with FTP hostname, username and
password as arguments
Ftp ftp = new Ftp("ftp.myserver.com","jsmith","secret");
// establish connection
ftp.connect();
Perform a directory listing
Once connected you may wish to perform a directory listing on
the FTP server. The directory listing will return a relative listing
of files and directories that are on the FTP server.
// perform directory listing
String listing = ftp.getDirListingAsString();
// print directory listing to console
System.out.println(listing);
Alternatively you may use the Ftp.getDirListing method. This method
parses the results and returns a java.util.Enumeration of FtpFile.
Using this method you may then iterate through the Enumeration and evaluate the contents of each file or directory programmatically.
Note: This method is only for use by FTP servers which display
directory listing using the UNIX or MS-DOS mode. For more information
see the Ftp.getDirListing method in the JavaDoc.
// Perform directory listing
Enumeration files = ftp.getDirListing();
// iterate through listing
while(files.hasMoreElements()) {
FtpFile file = (FtpFile)files.nextElement();
// only print files that are directories;
if(file.isDirectory()) {
System.out.println(file.getName());
}
}
Changing your local directory
Your local directory is the directory that you wish to use when
transferring files to / from the FTP server. By default this is
the directory that the Java executable was invoked from. To change
this setting simply invoke the Ftp.setLocalDir method.
// change local directory
ftp.setLocalDir(new File("C:/tmp");
Changing your remote directory
Your remote directory represents your current directory on the
FTP server. By default this is the directory that is assigned
when first logging into the FTP server. To change this setting
simply invoke the Ftp.setDir method.
// Change remote directory
ftp.setDir("temp");
Setting the transfer mode
Files transferred to / from an FTP server may be transmitted
using binary or ASCII modes. The binary transfer mode transfers
files without performing any conversion. Binary transfer is generally
used for files that are binary in nature such as images, executables
etc. The ASCII transfer mode transfers files converting new line
characters to the system dependent new line character used by
the operating system performing the transfer request. ASCII transfer
mode is generally used when downloading text only files. The default
transfer mode is binary.
// change transfer mode to ASCII
ftp.setAscii();
// change transfer mode to binary
ftp.setBinary();
Downloading files
To download a file simple invoke the Ftp.download method. File will
be transferred using the current transfer mode and saved in your
local directory. See Setting the transfer mode and Changing
your local directory for more information. There are other
methods that you may use for downloading files. These include
methods for downloading entire directories and downloading files
matching a regular expression. For more information see the JavaDoc.
// download a file named test.txt in remote directory
ftp.download("test.txt");
Uploading files
To upload a file simply invoke the Ftp.upload method. File will be
transferred using the current transfer mode and saved in your
remote directory. See Setting the transfer mode and Changing
your remote directory for more information. There are other
methods that you may use for uploading files. These include methods
for uploading entire directories and uploading files matching
a regular expression. For more information see the JavaDoc.
// upload a file named test.txt in c:/tmp directory
ftp.upload(new File("c:/tmp/test.txt"));
Releasing the connection
Once you have finished sending your email message(s) it is important
that you disconnect from the FTP server. This is easily accomplished
using the code below.
// release the FTP server connection
ftp.disconnect();
Summary
In this article you have learned the very basics needed to communicate
with an FTP server using Java. The FTP component provided in Secure FTP Factory makes this easy removing the need to understand all the
complexities of the FTP protocol.
|