Package com.simoncat.net

Source Code of com.simoncat.net.SendFileSFTP

package com.simoncat.net;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.io.UnsignedInteger32;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.sftp.FileAttributes;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.SftpClient;
import java.io.*;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;


import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;


public class SendFileSFTP {
  private String server;
  private String user;
  private String password;
  private int  portSSH;
 
  SshClient ssh;
  SftpClient sftp;
   
  public SendFileSFTP(String server,int portSSH, String usr,String pwd){
    this.server=server;
    this.user=usr;
    password=pwd;
    this.portSSH=portSSH;
  }

  public boolean connect(){
    // Make a client connection
    //System.out.println("Con1"); 
      ssh = new SshClient();
    //System.out.println("Con2"); 
      try
          // Connect to the host
      if(portSSH==0)
        ssh.connect(server,new IgnoreHostKeyVerification());
      else
            ssh.connect(server,portSSH,new IgnoreHostKeyVerification());
      //System.out.println("Con3"); 
          // Create a password authentication instance
          PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
      //System.out.println("Con4"); 
          pwd.setUsername(user);
          pwd.setPassword(password);
      //System.out.println("Con5"); 
          // Try the authentication
          int result = ssh.authenticate(pwd);
      //System.out.println("Con6 result:"+result); 
      // Evaluate the result
          if (result == AuthenticationProtocolState.COMPLETE) {
        //System.out.println("Con7"); 
        // The connection is authenticated we can now do some real work!
        sftp = ssh.openSftpClient();
        //System.out.println("Con8 sftp:"+sftp); 
        if(sftp==null)
          return false;
        else 
          return true;
          }
          else{
        //System.out.println("Con9"); 
        return false;
          }
      }
      catch(Exception e){
      e.printStackTrace();
      //System.out.println("Con10");   
      return false;
      }
       
  }


  public void disconnect(){
    try{
      sftp.quit();
      ssh.disconnect();
    }
    catch(Exception e){
      e.printStackTrace();
          }
  }
   

  public void send(File file,String remoteDir) {

  try {
    //sftp.mkdir("j2ssh");
    // Change directory
    sftp.cd(remoteDir);
    //System.out.println("Remote pwd:"+sftp.pwd());
    // Change the mode
    //sftp.chmod(0777, "j2ssh");
    //System.out.println("path file pwd:"+file.getPath()); 
    //sftp.lcd(file.getPath());
    //System.out.println("LOCAL pwd:"+sftp.lpwd()); 
    // Upload a file
    //System.out.println("LOCAL FILE:"+file.getName());   
    sftp.put(file.getPath());
  }
  catch (IOException ex) {
    ex.printStackTrace();
  }
   }
}
TOP

Related Classes of com.simoncat.net.SendFileSFTP

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.