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), SCP, and SFTP (FTP over SSH). Concurrent file transfers are supported via an underlying connection pool.

It is important to note that the SCP protocol is very limited in its operations. It can only do secure copy of files, and can therefore only be used for uploads and downloads. The protocol does not support directory navigation, file rename, delete and so on.

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

A single method call changes the protocol being used (although other configuration settings may need to be initialized). For examples that demonstrate how to use different protocols, see how to use SFTP with SecureFileTransferClient and how to use FTPS with SecureFileTransferClient. If changing to SCP, keep in mind the unique limitations imposed by the protocol: only uploads and downloads are supported.