Java Requires Chilkat v11.0.0+
Chilkat2 Java
Co:Z SFTP Binary File Download (from z/OS IBM Mainframe)
See more SFTP Examples
Demonstrates how to download a binary file, such as a .zip, from a Co:Z SFTP server on a z/OS IBM Mainframe.Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.SFtp;
import com.chilkatsoft.chilkat2.SFtpDir;
public class ChilkatExample {
public static void main(String[] args) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
SFtp sftp = new SFtp();
// Connect to the SSH server.
String hostname = "sftp.example.com";
int port = 22;
String handle;
try {
sftp.connect(hostname, port);
sftp.authenticatePw("myLogin", "myPassword");
sftp.initializeSftp();
// To download a binary file from the Co:Z SFTP server,
// we must switch to binary mode in the following unconventional way.
// We pretend to fetch a directory listing for "/+mode=binary"
// This has the effect of putting the server in binary mode for transfers.
handle = sftp.openDir("/+mode=binary");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Download the "directory listing" (but it's not actually a directory listing, and we'll just discard it.)
SFtpDir dirListing = new SFtpDir();
try {
sftp.readDirListing(handle, dirListing);
// Close the directory handle:
sftp.closeHandle(handle);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Download the binary file:
String localFilePath = "c:/temp/test.zip";
String remoteFilePath = "test.zip";
try {
sftp.downloadFileByName(remoteFilePath, localFilePath);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Success.");
}
}