How to upload a file to SFTP server using Zehon API ?

DeeKay July 5, 2012 at 1:42 am · SFTP, Zehon

Compared to the earlier SCP protocol, which allows only file transfers, the SFTP protocol allows for a range of operations on remote files – it is more like a remote file system protocol. An SFTP client’s extra capabilities compared to an SCP client include resuming interrupted transfers, directory listings, and remote file removal.

SFTP attempts to be more platform-independent than SCP; for instance, with SCP, the expansion of wildcards specified by the client is up to the server, whereas SFTP’s design avoids this problem. While SCP is most frequently implemented on Unix platforms, SFTP servers are commonly available on most platforms.

SFTP is not FTP run over SSH, but rather a new protocol designed from the ground up by the IETF SECSH working group. It is sometimes confused with Simple File Transfer Protocol.

Below java code shows how to upload a file to a SFTP server using Zehon API –

Please download Zehon SFTP API from Zehon Website. Also please support Zehon by donating or clicking on an Ads on Zehon website.

/****************************************************************************************
 * Created on Jul 4, 2012 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.zehon;

import com.zehon.FileTransferStatus;
import com.zehon.exception.FileTransferException;
import com.zehon.sftp.SFTP;

/**
 * Created by http://kodehelp.com
 * Date: Jul 4, 2012
 */
public class UploadFileToSFTPServer {

    /**
     * @param args
     */
    public static void main(String[] args) {
	String host = "10.20.30.40"; // Add your Host name or Host IP
	String username = "USERNAME"; // Your SFTP Server User Name
	String password = "PASSWORD"; // Your SFTP Server Password
	String destFolder = "/export/"; // SFTp destination directory where the file will be uploaded
	String filePath = "C:\\Sample.txt"; // absolute path of file to upload
	try {

		int uploadStatus = SFTP.sendFile(filePath, destFolder, host, username, password);
		if(FileTransferStatus.SUCCESS == uploadStatus){
			System.out.println(filePath + " got sftp-ed successfully to  folder "+destFolder);
		}
		else if(FileTransferStatus.FAILURE == uploadStatus){
			System.out.println("Fail to ssftp  to  folder "+destFolder);
		}
	} catch (FileTransferException e) {
		e.printStackTrace();
	}

    }

}