 |
 |
 |
 |
How to use SFTP (with the multi-protocol client)
|
 |
 |
 |
 |
At the most basic level of SFTP, assuming that the remote host, user and password are set,
all that is required is to set the protocol to SFTP,
which is done via the setProcotol method, as below:
The code above, by default, does not perform server validation. Server validation should always be enabled
for production machines, so that the wrong server is not communicated with.
To validate the server, the known_hosts
file must be loaded, via the loadSSHServerValidation method:
Numerous other SSH settings, including those for client validation, algorithms and server compatibility settings
can be accessed via the AdvancedSSHSettings class, accessed
by the getAdvancedSSHSettings() method.
Other configuration options are available via the AdvancedGeneralSettings class,
accessed by the getAdvancedSettings() method.
All of these configuration options should be set before the connect() method is called.
Example
The following example illustrates the use of SecureFileTransferClient for SFTP, using
both client and server validation. Public key authentication is used. Note that the client's
public key must be registered with the server.
// basic settings
SecureFileTransferClient client = new SecureFileTransferClient();
client.setRemoteHost(host);
client.setUserName(user);
client.setProtocol(Protocol.SFTP);
// server validation
client.loadSSHServerValidation(knownHostsFile);
// public key authentication - must set explicitly to this auth type
client.getAdvancedSSHSettings().setAuthenticationType(SSHAuthenticationType.PUBLIC_KEY);
client.getAdvancedSSHSettings().setPrivateKeyFile(clientKeyFile);
client.getAdvancedSSHSettings().setPrivateKeyFilePassphrase(clientPassphrase);
// connect
client.connect();
// do stuff
// disconnect from server
client.disconnect();