Monday, February 1, 2016

SFTP File Uploading/Downloading Using Java

 Recently I got a requirement to upload a file to host using a java program. I used Jsch library and below is the maven dependency required

 <dependency>  
       <groupId>com.jcraft</groupId>  
       <artifactId>jsch</artifactId>  
       <version>0.1.53</version>  
  </dependency>  


Using above library, below is a simple util class which you can use with SFTP on java.

 import com.jcraft.jsch.Channel;  
 import com.jcraft.jsch.ChannelSftp;  
 import com.jcraft.jsch.JSch;  
 import com.jcraft.jsch.JSchException;  
 import com.jcraft.jsch.ProxyHTTP;  
 import com.jcraft.jsch.Session;  
 import com.jcraft.jsch.SftpException;  
 /**  
  * Utility class to upload or download files via SFTP  
  *  
  * @author : chamara  
  */  
 public class SFTPFileUtil {  
      private Session session = null;  
      private ChannelSftp sftpChannel = null;  
      private String host = null;  
      private int port = 22;  
      private String username = null;  
      private String password = null;  
      private String strictHostChk = "no";  
      private final String SFTP_PROTOCOL = "sftp";  
      private boolean useHttpProxy = false;  
      private String httpProxyHost = null;  
      private int httpProxyPort;  
      public SFTPFileUtil(String host, String username, String password) {  
           this.host = host;  
           this.username = username;  
           this.password = password;  
      }  
      public SFTPFileUtil(String host, int port, String username, String password) {  
           this.host = host;  
           this.port = port;  
           this.username = username;  
           this.password = password;  
      }  
      public void connectAndLogin() throws JSchException {  
           if (session == null || !session.isConnected()) {  
                JSch jsch = new JSch();  
                session = jsch.getSession(username, host, port);  
                session.setConfig("StrictHostKeyChecking", strictHostChk);  
                session.setPassword(password);  
                if (isUseHttpProxy()) {  
                     session.setProxy(new ProxyHTTP(getHttpProxyHost(), getHttpProxyPort()));  
                }  
                session.connect();  
           }  
           if (sftpChannel == null || sftpChannel.isClosed()) {  
                Channel channel = session.openChannel(SFTP_PROTOCOL);  
                channel.connect();  
                sftpChannel = (ChannelSftp) channel;  
           }  
      }  
      public void closeAndLogout() {  
           if (sftpChannel != null) {  
                sftpChannel.exit();  
           }  
           if (session != null) {  
                session.disconnect();  
           }  
      }  
      @Override  
      protected void finalize() throws Throwable {  
           closeAndLogout();  
           super.finalize();  
      }  
      private boolean isUseHttpProxy() {  
           return useHttpProxy;  
      }  
      public void setHttpProxy(String httpHost, Integer httpPort) {  
           if (httpHost != null && !"".equals(httpHost.trim()) && httpPort != null && httpPort > 0) {  
                this.useHttpProxy = true;  
                this.httpProxyHost = httpHost;  
                this.httpProxyPort = httpPort;  
           } else {  
                this.useHttpProxy = false;  
           }  
      }  
      private String getHttpProxyHost() {  
           return httpProxyHost;  
      }  
      private int getHttpProxyPort() {  
           return httpProxyPort;  
      }  
      public void changeDirectory(String path) throws SftpException, JSchException {  
           connectAndLogin();  
           sftpChannel.cd(path);  
      }  
      public void uploadFile(String localPath) throws SftpException, JSchException {  
           connectAndLogin();  
           sftpChannel.put(localPath);  
      }  
      public void uploadFile(String localPath, String remotePath) throws SftpException, JSchException {  
           connectAndLogin();  
           sftpChannel.put(localPath, remotePath);  
      }  
      public void downloadFile(String localPath, String remotePath) throws SftpException, JSchException {  
           connectAndLogin();  
           sftpChannel.get(remotePath,localPath );  
      }  
      public static void main(String[] args){  
           SFTPFileUtil fileUtil = new SFTPFileUtil("[host name]","[user name]","[password]");  
           try {  
                fileUtil.uploadFile("[local file path]","[remote directory path]");  
           } catch (SftpException e) {  
                e.printStackTrace();  
           } catch (JSchException e) {  
                e.printStackTrace();  
           }  
           fileUtil.closeAndLogout();  
      }  
 }  

No comments:

Post a Comment