Return-path: <martin@ssf.com.mx>
Envelope-to: christian@ssf.com.mx
Delivery-date: Wed, 11 May 2016 15:45:41 -0500
Received: from [189.181.43.92] (port=47038 helo=[192.168.1.142])
	by vishnu.hosting-mexico.net with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128)
	(Exim 4.87)
	(envelope-from <martin@ssf.com.mx>)
	id 1b0b0n-0003me-3I
	for christian@ssf.com.mx; Wed, 11 May 2016 15:45:41 -0500
To: christian@ssf.com.mx
From: martin <martin@ssf.com.mx>
Subject: sftp file
Message-ID: <573399F6.6090800@ssf.com.mx>
Date: Wed, 11 May 2016 15:45:42 -0500
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.7.2
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

class SFTPinJava {

     public SFTPinJava() {
     }

/**
* @param args
*/
     public static void main(String[] args) {
         String SFTPHOST = "108.161.137.201";
             int    SFTPPORT = 2222;
             String SFTPUSER = "g4suser";

             String privateKey = "/home/martin/.ssh/id_rsa";
             String SFTPWORKINGDIR = "cpus";

             JSch jsch = new JSch();

             Session     session     = null;
             Channel     channel     = null;
             ChannelSftp channelSftp = null;

         try{
             jsch.addIdentity(privateKey);
             System.out.println("Private Key Added.");
             session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
             System.out.println("session created.");

             java.util.Properties config = new java.util.Properties();
             config.put("StrictHostKeyChecking", "no");
             session.setConfig(config);
             session.connect();
             channel = session.openChannel("sftp");

             channel.connect();
             System.out.println("shell channel connected....");

             channelSftp = (ChannelSftp)channel;
             channelSftp.cd(SFTPWORKINGDIR);
             byte[] buffer = new byte[1024];
             BufferedInputStream bis = new 
BufferedInputStream(channelSftp.get("list"));
             File newFile = new File("listdownload");
             OutputStream os = new FileOutputStream(newFile);
             BufferedOutputStream bos = new BufferedOutputStream(os);

             int readCount;

             while( (readCount = bis.read(buffer)) > 0) {
                 System.out.println("Writing: " );
                 bos.write(buffer, 0, readCount);
             }

             bis.close();
             bos.close();
             channel.disconnect();
             session.disconnect();
         }catch(Exception ex){

             ex.printStackTrace();
         }

     }

}

