Java FTP - Simple FTP using Java
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iTunes Gift Card Offer

For a limited time get a $50.00 iTunes gift card free with qualifying purchase.

Click for details.


News

JSCAPE Secure FTP Server 7.0 Released
08/20/2010 03:31 AM

JSCAPE launches Employee Giving Program
07/03/2010 08:26 AM

SSH Factory 3.6 Released
07/03/2010 06:22 AM

Secure iNet Factory 8.5 Released
07/03/2010 06:07 AM

Secure FTP Factory 8.5 Released
07/03/2010 06:03 AM

Secure FTP Applet 6.2 Released
06/10/2010 02:23 PM

more...


Tutorials

Enabling Phone Authentication
04/08/2009 11:24 AM

Detecting and Handling Brute Force Password Attacks
01/29/2009 09:44 AM

Creating a Domain
12/15/2008 11:33 AM

Public key authentication with SFTP
10/02/2008 07:59 AM

Formatting MimeMessages using .NET
09/14/2008 04:31 PM

Communicating with an IMAP4 server in .NET
09/14/2008 03:54 PM

more...


Articles

Open up corporate data to your partners
08/03/2010 10:01 PM

Access vital corporate documents on the go
07/01/2010 02:15 PM

SFTP and Encryption
05/17/2010 09:52 PM

Streamlining web uploads with ZIP archives
12/14/2009 10:11 AM

Using regular expressions in complex trigger conditions
09/08/2009 07:42 AM

Using custom forms to automate business processes
07/03/2009 08:51 AM

more...


Feedback

Request a feature or component

Request a Java or .NET component


 

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).

java ftp downloadjava ftp purchase

Related Products

Secure FTP Factory
JSCAPE Secure FTP Server
Secure FTP Applet

FTP (File Transfer Protocol)

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.