Documentation
Open Source Release Policy
Export Compliance
Using BouncyCastle
Installing the API
Enabling Logging
Per-Connection Logging
Third Party Dependencies
Generating SSH Keys
Using BouncyCastle
Using SSH Public Keys in Java
Supporting ED25519/Curve25519
Supporting Compression
Integrating Licensing into Maven Builds
Creating an SSH Client
Public Key Authentication
Keyboard Interactive Authentication
Public Key Authentication with sshagent
Executing Single Commands
Executing Commands within a Shell
Transferring Files
Connecting through Proxies
Display the Authentication Banner
Using the SFTP Client
Port Forwarding
Working Examples
Configuring Listening Interfaces
Configuring Host Keys
Password Authentication
Public Key Authentication
Challenge-Response Authentication
Configuring Port Forwarding
Configuring SFTP
Supporting SCP
Implementing your own File System
Creating an Interactive Terminal
Proxy Protocol Support
Implementing an SFTP Client
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 SftpClient.
Those familiar with the Maverick Legacy Client API will find the SftpClient similar to the SftpClient class that previously implemented SFTP.
You can use the following template in your application to connect, authenticate and start the SftpClientTask.
try (SshClient ssh = SshClientBuilder.create()
.withHostname("localhost")
.withPort(2222)
.withUsername("root")
.withPassword("xxxx")
.build()) {
SftpClient sftp = SftpClientBuilder.create()
.withClient(ssh)
.build();
}
Code language: JavaScript (javascript)
Transferring Data
To upload a file from the current file system, you can:
sftp.put("tmp.dmg");
Code language: CSS (css)
You can also pass OutputStream to receive the file content without going to the local file system.
ByteArrayOutputStream out = new ByteArrayOutputStream();
sftp.get("tmp.dmg", out);
Code language: JavaScript (javascript)
Or get an InputStream of the file content to process.
InputStream in = sftp.getInputStream("tmp.dmg");
Code language: JavaScript (javascript)
Similarly, you can pass an InputStream to write out data from your application held in an InputStream.
InputStream in = ....
sftp.put(in, "tmp.dmg");
Code language: JavaScript (javascript)
Or get an OutputStream to write content to the remote system.
OutputStream out = sftp.getOutputStream("tmp.dmg");
Code language: JavaScript (javascript)
Transferring with Patterns
The methods detailed so far only deal with a single file 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[] files = sftp.getFiles("*rfc*.txt");
Code language: JavaScript (javascript)
We can also do the same and putFiles
sftp.putFiles("*rfc&.txt");
Code language: CSS (css)
Matching is performed using Glob syntax by default. You can use a Perl Syntax for regular expressions also. To do this you will need to configure the pattern matcher first.
sftp.setRegularExpressionSyntax(SftpClientTask.Perl5Syntax);
Code language: CSS (css)
Changing Directory
You can change the user’s local directory by calling the following:
sftp.lcd("/tmp");
Code language: JavaScript (javascript)
Or change the remote directory using:
sftp.cd("/opt");
Code language: JavaScript (javascript)
To get the remote working directory
sftp.pwd();
Code language: CSS (css)
To get the local working directory
sftp.lpwd();
Code language: CSS (css)
Permissions
There are several methods for manipulating the permissions on the remote file system. Remember that Unix permissions are normally stated in an octal numbering system rather than decimal, so any value passed should be preceded by zero to indicate this to the JVM.
sftp.chmod(PosixPermissionsBuilder.create()
.fromBitmask(0644)
.build(), "tmp.dmg");
Code language: CSS (css)
You can change the owner or group of the file
sftp.chown("501", "tmp.dmg");
sftp.chgrp("501", "tmp.dmg");
Code language: JavaScript (javascript)
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, you should apply a umask setting. You can change the umask value using:
sftp.umask(0022);
Code language: CSS (css)
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 = sftp.stat("tmp.dmg")
Code language: JavaScript (javascript)
Or the attributes of a link
SftpFileAttributes attrs = sftp.statLink("tmp.dmg")
Code language: JavaScript (javascript)
If you want to change the attributes, get them using the above stat call and make modifications, then call
sftp.setAttributes("tmp.dmg", attrs);
Code language: CSS (css)
Symbolic Links
To get the target of a symbolic link
sftp.getSymbolicLinkTarget("linkpath");
Code language: JavaScript (javascript)
Moving and Deleting
To move a file you rename it.
sftp.rename("tmp.dmg", "new.dmg");
Code language: CSS (css)
To delete a file, use.
sftp.rm("tmp.dmg");
Code language: CSS (css)