Blog

Managed File Transfer and Network Solutions

Secure File Transfer with Java SCP Library

Posted by Charles Ott on Thu, Dec 15, 2011 @ 11:57 AM

java scpSCP is another old SSH (Secure Shell) file-transfer protocol — it can be traced back to the rcp command that was added to BSD Unix in 1982, which makes it the same age as ET The Extraterrestrial. It may have been the protocol ET used to communicate with his mothership. If he had had JSCAPE's Secure FTP Factory library, things might have gone more easily.

This example shows both the upload() and download() methods:

File Upload and Download with SCP

import java.io.*;
import com.jscape.inet.scp.Scp;
import com.jscape.inet.scp.events.*;
import com.jscape.inet.ssh.util.SshParameters;

public class SCPDemo implements ScpEventListener {

	private String hostname = "somewebsite.com";
	private String username = "jrandomuser";
	private String password = "ETCallHome";
	private String upDestination = "/";
	private String downDestination = "/";
	// This SshParameters instance is the same for upload or download.
	private SshParameters params = new SshParameters(hostname,username,password);
	
	public void doUpload(String upFile) throws Exception {
		// create new Scp instance
		Scp scp = new Scp(params);
		// register event listener
		scp.addListener(this);
		// establish connection (and disconnect, below)
		scp.connect();	
		// UPLOAD. The first argument is a File, the second is a String.
		scp.upload(new File(upFile),upDestination);
		scp.disconnect();
	}
	
	public void doDownload(String downFile) throws Exception {
		// create new Scp instance
		Scp scp = new Scp(params);
		// register event listener
		scp.addListener(this);
		// establish connection (and disconnect, below)
		scp.connect();	
		// DOWNLOAD. Both arguments are Strings.
		scp.download(downDestination, downFile);
		scp.disconnect();
	}
	
	// Various status messages.
	public void download(ScpFileDownloadedEvent evt) {
		System.out.println("Downloaded file: " + evt.getFilename());	
	}
	public void upload(ScpFileUploadedEvent evt) {
		System.out.println("Uploaded file: " + evt.getFilename());
	}
	public void progress(ScpTransferProgressEvent evt) {
		System.out.println("Transfer progress: " + evt.getFilename() + " " + evt.getTransferredBytes() + " bytes.");
	}
	public void connected(ScpConnectedEvent evt) {
		System.out.println("Connected to host: " + evt.getHost());
	}
	public void disconnected(ScpDisconnectedEvent evt) {
		System.out.println("Disconnected from host: " + evt.getHost());
	}
	
	public static void main(String[] args) {
		try {
			SCPDemo demonstrator = new SCPDemo();			

			// Ask for the file names to be uploaded or downloaded.
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			System.out.print("SCP Demo: Enter file to upload e.g. test.txt : ");
			String upFile = reader.readLine();
			if (upFile != "") { demonstrator.doUpload(upFile); }
			
			System.out.print("SCP Demo: Enter file to download e.g. test.txt : ");
			String downFile = reader.readLine();
			if (downFile != "") { demonstrator.doDownload(downFile); }
			
		} catch(Exception e) {
			e.printStackTrace();
		}		
	}
}

There are a few things to note about this example. First, we have registered the Scp instance with the addListener method, which lets us detect the salvo of messages this object shoots off when it uploads or downloads files. In this demo program, we do nothing but print these messages to the console, but your programs can make use of them to track the progress of transfers.

The filenames and destinations are paths relative to the program's directory. Also note that the upload and download methods have different signatures.

By the way, 1982 was also the year the band Survivor released Eye of the Tiger, and apparently both SCP and that song will live forever!

 

Would you like to try Secure FTP Factory for free?

 

 

Download an Evaluation of Secure FTP Factory

 

Topics: Secure FTP Factory, Secure iNet Factory