FTP Client

com.amoebacode.ftp
Class FTPClient

java.lang.Object
  extended bycom.amoebacode.ftp.FTPClient

public class FTPClient
extends java.lang.Object

FTPClient is a public class that handles many standard FTP client commands, including opening and closing connections, uploading and downloading files, creating and removing directories, renaming and deleting files, etc.

It can be used like this:

 FTPClient ftpClient = new FTPClient();
 try {
   ftpClient.openConnection(server, port);
   ftpClient.login(username, password);
   ftpClient.uploadFile(serverPath, localPath, asciiMode);
 } catch (IOException e) {
   e.printStackTrace();
 } finally {
   ftpClient.closeConnection();
 }

This class incorporates elements from Bret Taylor's FTPConection.


Field Summary
private static java.lang.String ANON_PASSWORD
          The password for anonymous login
private static java.lang.String ANON_USERNAME
          The username for anonymous login
private  boolean asciiTransfer
          Indicates whether this client is currently in ASCII transfer mode
private static int DEFAULT_PORT
          The default FTP port (21)
private  FTPSocket ftpSocket
          The object that handles all communication with the FTP server
private  java.lang.String nameListCommand
           
private static java.lang.String REMOTE_DIR_SEPARATOR
          The character that separates remote directories (/)
private static int SOCKET_TIMEOUT
          The default socket timeout (120 seconds)
 
Constructor Summary
FTPClient()
          Default public constructor creates an FTP client with debug mode turned off and the default socket timeout.
FTPClient(boolean debugMode)
          Public constructor creates an FTP client with the specified debug mode and the default socket timeout.
FTPClient(int socketTimeout, boolean debugMode)
          Public constructor creates an FTP client with the specified debug mode and socket timeout.
 
Method Summary
 void changeDirectory(java.lang.String directory)
          Changes the current FTP server directory to the specified (absolute or relative) path.
 void closeConnection()
          Disconnects this client from the FTP server.
 void deleteFile(java.lang.String fileName)
          Deletes the specified file from the FTP server.
private  void downloadDirectory(java.lang.String localPath, boolean ascii, boolean recursive)
          Downloads the current working directory from the FTP server to the specified local path using the specified transfer mode.
 void downloadDirectory(java.lang.String serverPath, java.lang.String localPath, boolean ascii, boolean recursive)
          Downloads the specified directory from the FTP server to the specified local path using the specified transfer mode.
 void downloadFile(java.lang.String fileName)
          Downloads the specified file from the FTP server to the current local working directory using the current client transfer mode.
 void downloadFile(java.lang.String serverPath, java.lang.String localPath)
          Downloads the specified file from the FTP server to the specified local path using the current client transfer mode.
 void downloadFile(java.lang.String serverPath, java.lang.String localPath, boolean ascii)
          Downloads the specified file from the FTP server to the specified local path using the specified transfer mode.
 void executeSiteCommand(java.lang.String siteArgument)
          Executes the SITE command on the FTP server with the given argument.
 java.lang.String getCurrentDirectory()
          Returns the current working directory on the FTP server.
 long getFileSize(java.lang.String fileName)
          Returns the size of the specified file on the FTP server.
 long getModificationTime(java.lang.String fileName)
          Returns the last modification time of the specified file on the FTP server.
 java.lang.String[] getNameArray()
          Returns a String array representing the files and directories contained in the current FTP server working directory.
 java.lang.String getNameList()
          Returns a new-line-delimited String representing the files and directories contained in the current FTP server working directory.
 java.lang.String getSystemType()
          Returns the system type of the FTP server.
 void login(java.lang.String username, java.lang.String password)
          Logs in this client to the FTP server using the specified username and password.
 void loginAnonymous()
          Logs in this client anonymously to the FTP server.
 void makeDirectory(java.lang.String directory)
          Creates a directory on the FTP server with the specified name.
 void openConnection(java.lang.String host)
          Opens a connection to the specified host through the default FTP port.
 void openConnection(java.lang.String host, int port)
          Opens a connection to the specified host through the specified port.
 void parentDirectory()
          Changes the current FTP server directory up one level to its parent directory.
 void removeDirectory(java.lang.String directory)
          Removes the specified directory (including its contents and subdirectories) from the FTP server.
private  void removeDirectoryContents()
          Removes all files and subdirectories from the current directory on the FTP server.
 void renameFile(java.lang.String oldName, java.lang.String newName)
          Renames the specified file on the FTP server with the specified name.
 void setLineSeparator(java.lang.String lineSeparator)
          Sets the line separator used for sending commands.
 void setNameListCommand(java.lang.String nameListCommand)
          Sets the name list command for this client.
 void setTransferType(boolean ascii)
          Sets the default transfer type (ASCII or binary) for this client.
 void uploadDirectory(java.lang.String serverPath, java.lang.String localPath, boolean ascii, boolean recursive)
          Uploads the specified directory from the local machine to the specified path of the FTP server using the specified transfer mode.
 void uploadDirectory(java.lang.String serverPath, java.lang.String localPath, boolean ascii, boolean recursive, java.io.FilenameFilter filenameFilter)
          Uploads files matching the specified filename filter in the specified directory from the local machine to the specified path of the FTP server using the specified transfer mode.
 void uploadFile(java.lang.String fileName)
          Uploads the specified file from the local machine to the current working directory of the FTP server using the current client transfer mode.
 void uploadFile(java.lang.String serverPath, java.lang.String localPath)
          Uploads the specified file from the local machine to the specified path of the FTP server using the current client transfer mode.
 void uploadFile(java.lang.String serverPath, java.lang.String localPath, boolean ascii)
          Uploads the specified file from the local machine to the specified path of the FTP server using the specified transfer mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ftpSocket

private FTPSocket ftpSocket
The object that handles all communication with the FTP server


asciiTransfer

private boolean asciiTransfer
Indicates whether this client is currently in ASCII transfer mode


nameListCommand

private java.lang.String nameListCommand

DEFAULT_PORT

private static final int DEFAULT_PORT
The default FTP port (21)

See Also:
Constant Field Values

SOCKET_TIMEOUT

private static final int SOCKET_TIMEOUT
The default socket timeout (120 seconds)

See Also:
Constant Field Values

REMOTE_DIR_SEPARATOR

private static final java.lang.String REMOTE_DIR_SEPARATOR
The character that separates remote directories (/)

See Also:
Constant Field Values

ANON_USERNAME

private static final java.lang.String ANON_USERNAME
The username for anonymous login

See Also:
Constant Field Values

ANON_PASSWORD

private static final java.lang.String ANON_PASSWORD
The password for anonymous login

See Also:
Constant Field Values
Constructor Detail

FTPClient

public FTPClient()
Default public constructor creates an FTP client with debug mode turned off and the default socket timeout.


FTPClient

public FTPClient(boolean debugMode)
Public constructor creates an FTP client with the specified debug mode and the default socket timeout.

Parameters:
debugMode - indicates whether server responses should be logged

FTPClient

public FTPClient(int socketTimeout,
                 boolean debugMode)
Public constructor creates an FTP client with the specified debug mode and socket timeout.

Parameters:
socketTimeout - specifies the socket timeout delay
debugMode - indicates whether server responses should be logged
Method Detail

openConnection

public void openConnection(java.lang.String host)
                    throws java.net.UnknownHostException,
                           java.io.IOException,
                           IllegalFTPResponseException
Opens a connection to the specified host through the default FTP port.

Parameters:
host - the host address of the FTP server
Throws:
java.net.UnknownHostException
java.io.IOException
IllegalFTPResponseException

openConnection

public void openConnection(java.lang.String host,
                           int port)
                    throws java.net.UnknownHostException,
                           java.io.IOException,
                           IllegalFTPResponseException
Opens a connection to the specified host through the specified port.

Parameters:
host - the host address of the FTP server
port - the FTP server connection port
Throws:
java.net.UnknownHostException
java.io.IOException
IllegalFTPResponseException

closeConnection

public void closeConnection()
                     throws java.io.IOException
Disconnects this client from the FTP server.

Throws:
java.io.IOException

login

public void login(java.lang.String username,
                  java.lang.String password)
           throws java.io.IOException,
                  IllegalFTPResponseException
Logs in this client to the FTP server using the specified username and password.

Parameters:
username - the FTP server username
password - the FTP server password
Throws:
java.io.IOException
IllegalFTPResponseException

loginAnonymous

public void loginAnonymous()
                    throws java.io.IOException,
                           IllegalFTPResponseException
Logs in this client anonymously to the FTP server.

Throws:
java.io.IOException
IllegalFTPResponseException

changeDirectory

public void changeDirectory(java.lang.String directory)
                     throws java.io.IOException,
                            IllegalFTPResponseException
Changes the current FTP server directory to the specified (absolute or relative) path.

Parameters:
directory - the new FTP server directory
Throws:
java.io.IOException
IllegalFTPResponseException

renameFile

public void renameFile(java.lang.String oldName,
                       java.lang.String newName)
                throws java.io.IOException,
                       IllegalFTPResponseException
Renames the specified file on the FTP server with the specified name.

Parameters:
oldName - the name of an existing file on the FTP server
newName - the new name for that file
Throws:
java.io.IOException
IllegalFTPResponseException

makeDirectory

public void makeDirectory(java.lang.String directory)
                   throws java.io.IOException,
                          IllegalFTPResponseException
Creates a directory on the FTP server with the specified name.

Parameters:
directory - the directory to create
Throws:
java.io.IOException
IllegalFTPResponseException

executeSiteCommand

public void executeSiteCommand(java.lang.String siteArgument)
                        throws java.io.IOException,
                               IllegalFTPResponseException
Executes the SITE command on the FTP server with the given argument.

Throws:
java.io.IOException
IllegalFTPResponseException

removeDirectory

public void removeDirectory(java.lang.String directory)
                     throws java.io.IOException,
                            IllegalFTPResponseException
Removes the specified directory (including its contents and subdirectories) from the FTP server.

Parameters:
directory - the directory to delete
Throws:
java.io.IOException
IllegalFTPResponseException

removeDirectoryContents

private void removeDirectoryContents()
                              throws java.io.IOException,
                                     IllegalFTPResponseException
Removes all files and subdirectories from the current directory on the FTP server.

Throws:
java.io.IOException
IllegalFTPResponseException

parentDirectory

public void parentDirectory()
                     throws java.io.IOException,
                            IllegalFTPResponseException
Changes the current FTP server directory up one level to its parent directory.

Throws:
java.io.IOException
IllegalFTPResponseException

deleteFile

public void deleteFile(java.lang.String fileName)
                throws java.io.IOException,
                       IllegalFTPResponseException
Deletes the specified file from the FTP server.

Parameters:
fileName - the file to delete
Throws:
java.io.IOException
IllegalFTPResponseException

getCurrentDirectory

public java.lang.String getCurrentDirectory()
                                     throws java.io.IOException
Returns the current working directory on the FTP server.

Returns:
the path to the current working directory
Throws:
java.io.IOException

getSystemType

public java.lang.String getSystemType()
                               throws java.io.IOException
Returns the system type of the FTP server.

Returns:
the FTP server system type
Throws:
java.io.IOException

getModificationTime

public long getModificationTime(java.lang.String fileName)
                         throws java.io.IOException
Returns the last modification time of the specified file on the FTP server.

Parameters:
fileName - the file to check
Returns:
the last modification time of that file
Throws:
java.io.IOException

getFileSize

public long getFileSize(java.lang.String fileName)
                 throws java.io.IOException
Returns the size of the specified file on the FTP server.

Parameters:
fileName - the file to check
Returns:
the size of that file
Throws:
java.io.IOException

downloadFile

public void downloadFile(java.lang.String fileName)
                  throws java.io.IOException,
                         IllegalFTPResponseException
Downloads the specified file from the FTP server to the current local working directory using the current client transfer mode.

Parameters:
fileName - the file to download
Throws:
java.io.IOException
IllegalFTPResponseException

downloadFile

public void downloadFile(java.lang.String serverPath,
                         java.lang.String localPath)
                  throws java.io.IOException,
                         IllegalFTPResponseException
Downloads the specified file from the FTP server to the specified local path using the current client transfer mode.

Parameters:
serverPath - the file to download
localPath - the local file destination
Throws:
java.io.IOException
IllegalFTPResponseException

downloadFile

public void downloadFile(java.lang.String serverPath,
                         java.lang.String localPath,
                         boolean ascii)
                  throws java.io.IOException,
                         IllegalFTPResponseException
Downloads the specified file from the FTP server to the specified local path using the specified transfer mode.

Parameters:
serverPath - the file to download
localPath - the local file destination
ascii - indicates whether ascii transfer mode should be used
Throws:
java.io.IOException
IllegalFTPResponseException

downloadDirectory

public void downloadDirectory(java.lang.String serverPath,
                              java.lang.String localPath,
                              boolean ascii,
                              boolean recursive)
                       throws java.io.IOException,
                              IllegalFTPResponseException
Downloads the specified directory from the FTP server to the specified local path using the specified transfer mode.

Parameters:
serverPath - the directory to download
localPath - the local directory destination
ascii - indicates whether ascii transfer mode should be used
recursive - indicates whether subdirectories should be downloaded
Throws:
java.io.IOException
IllegalFTPResponseException

downloadDirectory

private void downloadDirectory(java.lang.String localPath,
                               boolean ascii,
                               boolean recursive)
                        throws java.io.IOException,
                               IllegalFTPResponseException
Downloads the current working directory from the FTP server to the specified local path using the specified transfer mode.

Parameters:
localPath - the local directory destination
ascii - indicates whether ascii transfer mode should be used
recursive - indicates whether subdirectories should be downloaded
Throws:
java.io.IOException
IllegalFTPResponseException

uploadFile

public void uploadFile(java.lang.String fileName)
                throws java.io.IOException,
                       IllegalFTPResponseException
Uploads the specified file from the local machine to the current working directory of the FTP server using the current client transfer mode.

Parameters:
fileName - the file to upload
Throws:
java.io.IOException
IllegalFTPResponseException

uploadFile

public void uploadFile(java.lang.String serverPath,
                       java.lang.String localPath)
                throws java.io.IOException,
                       IllegalFTPResponseException
Uploads the specified file from the local machine to the specified path of the FTP server using the current client transfer mode.

Parameters:
serverPath - the FTP server destination
localPath - the file to upload
Throws:
java.io.IOException
IllegalFTPResponseException

uploadFile

public void uploadFile(java.lang.String serverPath,
                       java.lang.String localPath,
                       boolean ascii)
                throws java.io.IOException,
                       IllegalFTPResponseException
Uploads the specified file from the local machine to the specified path of the FTP server using the specified transfer mode.

Parameters:
serverPath - the FTP server destination
localPath - the file to upload
ascii - indicates whether ascii transfer mode should be used
Throws:
java.io.IOException
IllegalFTPResponseException

uploadDirectory

public void uploadDirectory(java.lang.String serverPath,
                            java.lang.String localPath,
                            boolean ascii,
                            boolean recursive)
                     throws java.io.IOException,
                            IllegalFTPResponseException
Uploads the specified directory from the local machine to the specified path of the FTP server using the specified transfer mode.

Parameters:
serverPath - the FTP server destination
localPath - the directory to upload
ascii - indicates whether ascii transfer mode should be used
recursive - indicates whether subdirectories should be uploaded
Throws:
java.io.IOException
IllegalFTPResponseException

uploadDirectory

public void uploadDirectory(java.lang.String serverPath,
                            java.lang.String localPath,
                            boolean ascii,
                            boolean recursive,
                            java.io.FilenameFilter filenameFilter)
                     throws java.io.IOException,
                            IllegalFTPResponseException
Uploads files matching the specified filename filter in the specified directory from the local machine to the specified path of the FTP server using the specified transfer mode.

Parameters:
serverPath - the FTP server destination
localPath - the directory to upload
ascii - indicates whether ascii transfer mode should be used
recursive - indicates whether subdirectories should be uploaded
filenameFilter - indicates which files should be uploaded
Throws:
java.io.IOException
IllegalFTPResponseException

getNameArray

public java.lang.String[] getNameArray()
                                throws java.io.IOException,
                                       IllegalFTPResponseException
Returns a String array representing the files and directories contained in the current FTP server working directory.

Returns:
the contents of the FTP server working directory
Throws:
java.io.IOException
IllegalFTPResponseException

getNameList

public java.lang.String getNameList()
                             throws java.io.IOException,
                                    IllegalFTPResponseException
Returns a new-line-delimited String representing the files and directories contained in the current FTP server working directory.

Returns:
the contents of the FTP server working directory
Throws:
java.io.IOException
IllegalFTPResponseException

setTransferType

public void setTransferType(boolean ascii)
Sets the default transfer type (ASCII or binary) for this client.

Parameters:
ascii - the new default transfer type

setNameListCommand

public void setNameListCommand(java.lang.String nameListCommand)
Sets the name list command for this client. The default is set to "nlst" (the value from FTPCommandRegistry.NAME_LIST_COMMAND), but it can be changed to values such as "nlst -a" (from CustomFTPCommandRegistry.WU_FTP_NAME_LIST_COMMAND) for WU-FTP servers.

Parameters:
nameListCommand - the new name list command

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Sets the line separator used for sending commands. Default value is the "line.separator" System property.

Parameters:
lineSeparator - the new line separator String

FTP Client