|
FTP Client | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.amoebacode.ftp.FTPClient
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 |
private FTPSocket ftpSocket
private boolean asciiTransfer
private java.lang.String nameListCommand
private static final int DEFAULT_PORT
private static final int SOCKET_TIMEOUT
private static final java.lang.String REMOTE_DIR_SEPARATOR
private static final java.lang.String ANON_USERNAME
private static final java.lang.String ANON_PASSWORD
| Constructor Detail |
public FTPClient()
public FTPClient(boolean debugMode)
debugMode - indicates whether server responses should be logged
public FTPClient(int socketTimeout,
boolean debugMode)
socketTimeout - specifies the socket timeout delaydebugMode - indicates whether server responses should be logged| Method Detail |
public void openConnection(java.lang.String host)
throws java.net.UnknownHostException,
java.io.IOException,
IllegalFTPResponseException
host - the host address of the FTP server
java.net.UnknownHostException
java.io.IOException
IllegalFTPResponseException
public void openConnection(java.lang.String host,
int port)
throws java.net.UnknownHostException,
java.io.IOException,
IllegalFTPResponseException
host - the host address of the FTP serverport - the FTP server connection port
java.net.UnknownHostException
java.io.IOException
IllegalFTPResponseException
public void closeConnection()
throws java.io.IOException
java.io.IOException
public void login(java.lang.String username,
java.lang.String password)
throws java.io.IOException,
IllegalFTPResponseException
username - the FTP server usernamepassword - the FTP server password
java.io.IOException
IllegalFTPResponseException
public void loginAnonymous()
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseException
public void changeDirectory(java.lang.String directory)
throws java.io.IOException,
IllegalFTPResponseException
directory - the new FTP server directory
java.io.IOException
IllegalFTPResponseException
public void renameFile(java.lang.String oldName,
java.lang.String newName)
throws java.io.IOException,
IllegalFTPResponseException
oldName - the name of an existing file on the FTP servernewName - the new name for that file
java.io.IOException
IllegalFTPResponseException
public void makeDirectory(java.lang.String directory)
throws java.io.IOException,
IllegalFTPResponseException
directory - the directory to create
java.io.IOException
IllegalFTPResponseException
public void executeSiteCommand(java.lang.String siteArgument)
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseException
public void removeDirectory(java.lang.String directory)
throws java.io.IOException,
IllegalFTPResponseException
directory - the directory to delete
java.io.IOException
IllegalFTPResponseException
private void removeDirectoryContents()
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseException
public void parentDirectory()
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseException
public void deleteFile(java.lang.String fileName)
throws java.io.IOException,
IllegalFTPResponseException
fileName - the file to delete
java.io.IOException
IllegalFTPResponseException
public java.lang.String getCurrentDirectory()
throws java.io.IOException
java.io.IOException
public java.lang.String getSystemType()
throws java.io.IOException
java.io.IOException
public long getModificationTime(java.lang.String fileName)
throws java.io.IOException
fileName - the file to check
java.io.IOException
public long getFileSize(java.lang.String fileName)
throws java.io.IOException
fileName - the file to check
java.io.IOException
public void downloadFile(java.lang.String fileName)
throws java.io.IOException,
IllegalFTPResponseException
fileName - the file to download
java.io.IOException
IllegalFTPResponseException
public void downloadFile(java.lang.String serverPath,
java.lang.String localPath)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the file to downloadlocalPath - the local file destination
java.io.IOException
IllegalFTPResponseException
public void downloadFile(java.lang.String serverPath,
java.lang.String localPath,
boolean ascii)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the file to downloadlocalPath - the local file destinationascii - indicates whether ascii transfer mode should be used
java.io.IOException
IllegalFTPResponseException
public void downloadDirectory(java.lang.String serverPath,
java.lang.String localPath,
boolean ascii,
boolean recursive)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the directory to downloadlocalPath - the local directory destinationascii - indicates whether ascii transfer mode should be usedrecursive - indicates whether subdirectories should be downloaded
java.io.IOException
IllegalFTPResponseException
private void downloadDirectory(java.lang.String localPath,
boolean ascii,
boolean recursive)
throws java.io.IOException,
IllegalFTPResponseException
localPath - the local directory destinationascii - indicates whether ascii transfer mode should be usedrecursive - indicates whether subdirectories should be downloaded
java.io.IOException
IllegalFTPResponseException
public void uploadFile(java.lang.String fileName)
throws java.io.IOException,
IllegalFTPResponseException
fileName - the file to upload
java.io.IOException
IllegalFTPResponseException
public void uploadFile(java.lang.String serverPath,
java.lang.String localPath)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the FTP server destinationlocalPath - the file to upload
java.io.IOException
IllegalFTPResponseException
public void uploadFile(java.lang.String serverPath,
java.lang.String localPath,
boolean ascii)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the FTP server destinationlocalPath - the file to uploadascii - indicates whether ascii transfer mode should be used
java.io.IOException
IllegalFTPResponseException
public void uploadDirectory(java.lang.String serverPath,
java.lang.String localPath,
boolean ascii,
boolean recursive)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the FTP server destinationlocalPath - the directory to uploadascii - indicates whether ascii transfer mode should be usedrecursive - indicates whether subdirectories should be uploaded
java.io.IOException
IllegalFTPResponseException
public void uploadDirectory(java.lang.String serverPath,
java.lang.String localPath,
boolean ascii,
boolean recursive,
java.io.FilenameFilter filenameFilter)
throws java.io.IOException,
IllegalFTPResponseException
serverPath - the FTP server destinationlocalPath - the directory to uploadascii - indicates whether ascii transfer mode should be usedrecursive - indicates whether subdirectories should be uploadedfilenameFilter - indicates which files should be uploaded
java.io.IOException
IllegalFTPResponseException
public java.lang.String[] getNameArray()
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseException
public java.lang.String getNameList()
throws java.io.IOException,
IllegalFTPResponseException
java.io.IOException
IllegalFTPResponseExceptionpublic void setTransferType(boolean ascii)
ascii - the new default transfer typepublic void setNameListCommand(java.lang.String nameListCommand)
nameListCommand - the new name list commandpublic void setLineSeparator(java.lang.String lineSeparator)
lineSeparator - the new line separator String
|
FTP Client | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||