In Transferring Files, we covered how to use the high-level API of SshClient to perform basic upload and download of files. To provide a more rich SFTP experience the Maverick Synergy API provides the SftpClientTask
For those of you familiar with the Maverick Legacy Client API you will find the SFTP task similar to the SftpClient class that previously implement SFTP.
To get started you can use the following template in your application to connect, authenticate and start the SftpClientTask. All you need to do is to place your SFTP operations inside the doSftp method:
try(SshClient ssh = new SshClient("localhost", 22, "lee", "xxxxxx".toCharArray())) {
runTask(new SftpClientTask(ssh) {
protected void doSftp() {
}
});
}
Transferring Data
To upload a file from the current file system you can:
put("tmp.dmg");
You can also pass OutputStream to receive the file content without going to the local file system.
ByteArrayOutputStream out = new ByteArrayOutputStream(); get("tmp.dmg", out);
Or get an InputStream of the file content to process.
InputStream in = getInputStream("tmp.dmg");
Similarly, you can pass an InputStream to write out data from your application held in an InputStream.
InputStream in = .... put(in, "tmp.dmg");
Or get an OutputStream to write content to the remote system.
OutputStream out = getOutputStream("tmp.dmg");
Transfering with Patterns
The methods detailed so far only deal with single files at a time. For example, we can download all files with a specific pattern using the getFiles method. This returns an array of SftpFile objects that have been transferred.
SftpFile[] getFiles("*rfc*.txt");
We can also do the same and putFiles
putFiles("*rfc&.txt");
Matching is performed using Glob syntax by default. You can use a Perl Syntax for regular expressions also. Do do this you will need to configure the pattern matcher first.
setRegularExpressionSyntax(SftpClientTask.Perl5Syntax);
Changing Directory
You can change the users local directory by calling the following inside the doTask method:
lcd("/tmp");
Or change the remote directory using:
cd("/opt");
To get the remote working directory
pwd();
To get the local working directory
lpwd();
Permissions
There are a number of methods for manipulating the permissions on the remote file system. Remember that unix permissions are normally stated in octal numbering system rather than decimal so any value passed should be preceded with zero to indicate this to the JVM.
chmod(0644, "tmp.dmg");
You can change the owner or group of the file
chown("501", "tmp.dmg");
chgrp("501", "tmp.dmg");
In SFTP version 3 or below the UID/GID expected is normally the numerical ID and not the user or group name. Version 4 or above support names. The methods take a string to allow both to be passed.
By default, a umask is not applied to files and folders created remotely. If you do not want the server to decide the permissions then you should apply a umask setting. You can change the umask value using:
umask(0022);
This will apply the default permissions of 0666 ^ 0022 for new files, and 0777 ^ 0022 for new folders.
File Attributes
You can get the current SftpFileAttributes of a file using a stat command.
SftpFileAttributes attrs = stat("tmp.dmg")
Or the attributes of a link
SftpFileAttributes attrs = statLink("tmp.dmg")
If you want to change the attributes, get them using the above stat call and make modifications, then call
setAttributes("tmp.dmg", attrs);
Symbolic Links
To get the target of a symbolic link
getSymbolicLinkTarget("linkpath");
Moving and Deleting
To move a file you rename it.
rename("tmp.dmg", "new.dmg");
To delete a file use.
rm("tmp.dmg");