SFTP for Java


SFTP Tutorial

Table of content


# About Zehon SFTP

Zehon SFTP for Java is a versatile file-transfer component for Java language. It enables you to transfer files directly from your application using SFTP, a powerful and secure file-transfer protocol that runs over an SSH session. SSH and SFTP are nearly ubiquitous on Unix and Unix-like systems, with OpenSSH the most common implementation. There is also a growing number of SSH/SFTP servers for Windows as well.

Please note that Zehon SFTP doesn't implement the FTPS protocol, also known as FTP over SSL or FTP over TLS - if this is what you need, please check out our Secure FTP component instead.

 

back to top...


# Uploading and downloading files

File transfers are an essential part of the SFTP protocol and can be achieved using the sendFile and getFile methods. They accept the path to the local file and the path to the remote file (both paths must include the filename) and return the number of bytes transferred (as long integer).   Please prefer to the Java Doc for more information.

Short way (uploading a file):

// upload C:\myfiles\test.txt to remote folder \mysftpFolder 
//with sftp server sftp.myhost.com, username sftp and password pass
     
try{    
  int status = SFTP.sendFile("C:\myfiles\test.txt", "/mysftpFolder", "sftp.myhost.com", 
                                                             "sftp", "pass");
}catch(FileTransferException ex){
  e.printStackTrace();
}  


Longer way (uploading a file):

// upload C:\myfiles\test.txt to remote folder \mysftpFolder 
//with sftp server sftp.myhost.com, username sftp and password pass
     
SFTPClient sftpClient = new SFTPClient("sftp.myhost.com","sftp", "pass");
try{    
  int status = SFTP.sendFile("C:\myfiles\test.txt", "/mysftpFolder");
}catch(FileTransferException ex){
  e.printStackTrace();
}  

Unlike the FTP protocol, SFTP allows multiple concurrent operations at the same time. This makes it perfectly possible to transfer several files simultaneously over a single session and even browse directories at the same time. However, some buggy SFTP servers have problems with this, even though this is a standard SFTP feature.

Short way (uploading a file represented by java.io.InputStream):

// upload an java.io.InputStream representing C:\myfiles\test.txt to 
//remote folder \mysftpFolder 
//with sftp server sftp.myhost.com, username sftp and password pass
     

InputStream is = null;
String filePath = "C:\\myfiles\\test.txt";
File localFile = new File(filePath);        
try {
	is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}
String destFolder = "/mysftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
   
	status = SFTP.sendFile(is, nameOfFile, destFolder, 
	                            "sftp.myhost.com", "sftp", "pass" );
} catch (FileTransferException e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  (
	//Depending on what version of JDK you use)			
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}


Longer way (uploading a file represented by java.io.InputStream):

// upload an java.io.InputStream representing C:\myfiles\test.txt to 
//remote folder \mysftpFolder 
//with sftp server sftp.myhost.com, username sftp and password pass
     
InputStream is = null;
String filePath = "C:\\myfiles\\test.txt";
File localFile = new File(filePath);        
try {
	is = new BufferedInputStream(new FileInputStream(localFile));
} catch (FileNotFoundException e1) {
	e1.printStackTrace();
}
String destFolder = "/mysftpFolder";
String nameOfFile = "testStream.txt";
int status = -1;
try {
    SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass" );
	status = sftpClient.sendFile(is, nameOfFile, destFolder);
} catch (FileTransferException e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)			
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

 

Short way (downloading a file):

// downloading testStream.txt from remote folder \test
//to local folder C:\\myfiles\\writeToFolder
//with sftp server sftp.myhost.com, username sftp and password pass
     
String sftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {		    
	status = SFTP.getFile(nameOfFile, sftpFolder, toLocalFolder,
	                                 "sftp.myhost.com", "sftp", "pass");
	
} catch (Exception e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)					
}

Longer way (downloading a file):

// downloading testStream.txt from remote folder \test
//to local folder C:\\myfiles\\writeToFolder
//with sftp server sftp.myhost.com, username sftp and password pass
     
String sftpFolder = "/test";
String nameOfFile = "testStream.txt";
String toLocalFolder = "C:\\myfiles\\writeToFolder";
int status = -1;
try {
    SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
	status = sftpClient.getFile(nameOfFile, sftpFolder, toLocalFolder);
	
} catch (Exception e) {
	e.printStackTrace();
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)					
}

Unlike the FTP protocol, SFTP allows multiple concurrent operations at the same time. This makes it perfectly possible to transfer several files simultaneously over a single session and even browse directories at the same time. However, some buggy SFTP servers have problems with this, even though this is a standard SFTP feature.

Short way (downloading a file represented by java.io.InputStream):

// downloading testStream.txt from remote folder \test
//the result is java.io.InputStream
//with sftp server sftp.myhost.com, username sftp and password pass
     

InputStream is = null;		
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
	
	is = SFTP.getFileAsStream(nameOfFile, destFolder, 
	                            "sftp.myhost.com", "sftp", "pass" );
} catch (Exception e) {
	e.printStackTrace();			
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

Longer way (downloading a file represented by java.io.InputStream):

// downloading testStream.txt from remote folder \test
//the result is java.io.InputStream
//with sftp server sftp.myhost.com, username sftp and password pass
     
InputStream is = null;		
String destFolder = "/test";
String nameOfFile = "testStream.txt";
try {
	SFTPClient sftpClient = new SFTPClient("sftp.myhost.com", "sftp", "pass");
	is = sftpClient.getFileAsStream(nameOfFile, destFolder);			
} catch (Exception e) {
	e.printStackTrace();			
	//or
	//ex.getCause().printStackTrace();  
	//(Depending on what version of JDK you use)
}finally{
	if(is != null){try {is.close();} catch (IOException e) {}}
}

 

back to top...



# Working with directories

Working with directories (folders) on the SFTP server is simple. The remote filesystem is organized in the same way as in Un*x. If you are used to Windows, watch out for the two differencies - a slash (/) is used instead of a backslash, and there is only a single root at "/", no drive letters. A typical path to a file might look like "/pub/incoming/test.zip", for example.

Coming soon! Please prefer to the JavaDoc for more information.

back to top...


# List of files and directories

The following code snippet dispalys the list of files in the remote directory to a console:

Coming soon!Please prefer to the JavaDoc for more information.

back to top...


# Transfering multiple files and directories

Upload or download of multiple files is a very common task. There are getFolder and sendFolder methods that can be used to transfer multiple files easily - just provide the source path (which can be a directory or contain wildcards), destination path and transfer options.

When transferring lots of files, things can occasionally go wrong due to unforeseen problems - to be informed about such errors, use BatchTransferProgress event that also makes it possible to select the desired next action. To stay informed about what is currently going on, use the BatchTransferProgress event.

For more information about these events, check out the BatchTransfer sample application!

Coming soon! Please prefer to the JavaDoc for more information.

 

back to top...