Wednesday, September 29, 2010

SFTP With com.jcraft.jsch

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;
import java.util.Vector;
import org.apache.log4j.Logger;

public class SCPUtil
{
  private static final Logger _logger = Logger.getLogger(SCPUtil.class);

  static FileInputStream utilConf = null;
  static Properties props = null;

  public static void main(String[] args) {
    if (args.length < 1)
    {
      System.out.println("Usage SCPUtil <config file name> ");
    }
    else
    {
      System.out.println("Property File " + args[0]);
      try {
        utilConf = new FileInputStream(new File(args[0]));
        props = new Properties();
        props.load(utilConf);
        String server = props.getProperty("hostname");
        String loginId = props.getProperty("login");
        System.out.println(server);
        String passwd = props.getProperty("passowrd");
        String localdrv = props.getProperty("localdrv");
        String remotedrv = props.getProperty("remotedrv");
        String file = props.getProperty("file");

        get(server, loginId, passwd, localdrv, remotedrv, file);
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }

  public static void get(String server, String loginId, String passwd, String localdrv, String remotedrv, String file)
    throws Exception
  {
    boolean downloadStatus = false;
    Session session = null;
    try
    {
      JSch jsch = new JSch();
      System.out.println(loginId);
      session = jsch.getSession(loginId, server, 22);
      UserInfo ui = new MyUserInfo();
      session.setUserInfo(ui);
      session.setPassword(passwd);
      session.connect();
      session.getHost();
      session.getHostKey();
      _logger.info("Connected to [" + server + "] with login " + loginId);
      Channel channel = session.openChannel("sftp");
      channel.connect();
      _logger.info("Connected to Server :::" + server);
      ChannelSftp c = (ChannelSftp)channel;
      c.cd(remotedrv);
      _logger.info("Remote address " + remotedrv);
      Vector lst = c.ls(remotedrv);
      for (int i = 0; i < lst.size(); i++)
      {
        ChannelSftp.LsEntry le = (ChannelSftp.LsEntry)lst.get(i);
        _logger.info("if dirctory :::" + le.getAttrs().isDir());
        if (le.getAttrs().isDir())
          continue;
        String fName1 = le.getFilename();
        _logger.info("fName1::::" + fName1);
        System.out.println("file name " + fName1);
        if (!fName1.equals(file))
          continue;
        downloadStatus = true;
        _logger.info("Downloading" + fName1);
        System.out.println("Downloading file name ::" + fName1);
        c.get(fName1, localdrv);
        String name = null;
        System.out.println("Downloaded file name ::" + fName1);
      }

      if (!downloadStatus)
        throw new Exception("File not Found in Remote Directory", new FileNotFoundException());
      c.quit();
    }
    catch (SftpException e) {
      _logger.fatal("Problem with SFTP -" + e.getMessage());
      throw e;
    } catch (JSchException e) {
      _logger.fatal("Problem with Connection  -" + e.getMessage());
      throw e;
    }
    catch (Exception e) {
      _logger.fatal("Unknown Exception -" + e.getMessage());
      throw e;
    }
    finally
    {
      if (session != null)
        session.disconnect();
    }
  }

  public static class MyUserInfo
    implements UserInfo
  {
    public String getPassphrase()
    {
      return null;
    }

    public String getPassword()
    {
      return null;
    }

    public boolean promptPassword(String a)
    {
      return true;
    }

    public boolean promptPassphrase(String a)
    {
      return true;
    }

    public boolean promptYesNo(String a)
    {
      return true;
    }

    public void showMessage(String s)
    {
    }
  }
}

No comments:

Post a Comment