 |
 |
 |
 |
How to use the multi-protocol client
|
 |
 |
 |
 |
SecureFileTransferClient
is a powerful, easy-to-use file transfer client that supports multiple protocols, including FTP, FTPS (FTP over SSL), and
SFTP (FTP over SSH). Concurrent file transfers are supported via an underlying connection pool.
The examples below demonstrate how basic operations are performed using this class.
Example - connection and disconnection
This example demonstrates connecting to a server, and disconnection.
// basic settings
SecureFileTransferClient client = new SecureFileTransferClient();
client.setRemoteHost(host);
client.setUserName(user);
client.setPassword(password);
client.setProtocol(Protocol.FTP); // FTP is the default
// connect
client.connect();
// do whatever
// disconnect from server
client.disconnect();
It is important to note that when connect() is called, a thread pool and a connection pool are
created for servicing requests. The disconnect() method must be called to terminate
the pools. If disconnect() is not called, applications will not exit.
Example - uploading and downloading files
This example demonstrates uploading a file to the server, downloading it to a file
of a different name, and then deleting the file from the server. The file is transferred
in ASCII mode.
// set to ASCII mode transfers
client.setContentType(FTPTransferType.ASCII);
// transfer the file
client.uploadFile(localFileName, remoteFileName);
client.downloadFile(localFileName + ".copy", remoteFileName);
client.deleteFile(remoteFileName);
Example - list a directory
This example demonstrates changing into a directory on the server, and then listing the
directory.
// change into the directory
client.changeDirectory(directoryToList);
// list the directory
FTPFile[] files = client.directoryList();
for (int i = 0; i < files.length; i++) {
System.out.println(files[i].toString());
}
Changing protocols