import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;
/ **
*
* @author ccascante
* /
public class Main {
static final String USERNAME = "mobile";
static String HOST = "201.218.222.7";
static int PORT = 55507;
static String PASSWORD = "transferencias2$";
static String REMOTEPATH ="/mobile/java/";/*Agregar el working file para la descarga*/
static String LOCALPATH="C:/Cargar/SFTP/";/*Agregar el working file para la descarga*/
static String FILE="Avellanas.bmp";/*Archivo que deseamos descargar*/
public static void Descargar(String pUser, String pPass,String pHost, int pPort, String localpath, String remotepath, String file)
throws Exception {
JSch sftp = new JSch();
// Instancio el objeto session para la transferencia
Session session = null;
// instancio el canal sftp
ChannelSftp channelSftp = null;
try {
// Inciciamos el JSch con el usuario, host y puerto
session = sftp.getSession(pUser, pHost, pPort);
// Seteamos el password
session.setPassword(pPass);
// El SFTP requiere un intercambio de claves
// con esta propiedad le decimos que acepte la clave
// sin pedir confirmación
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// Abrimos el canal de sftp y conectamos
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
System.out.println( ""+channelSftp.isConnected());
// Convertimos el archivo a transferir en un OutputStream donde se va a guardar
OutputStream os = new BufferedOutputStream(new FileOutputStream(
localpath+'/'+file));
// Iniciamos la transferencia
channelSftp.get(remotepath+'/'+file, os);
} catch (JSchException e) {
throw new Exception(e);
} catch (SftpException e){
throw new Exception(e);
} catch (FileNotFoundException e){
throw new Exception(e);
}finally{
// Cerramos el canal y session
if (channelSftp.isConnected())
channelSftp.disconnect();
if (session.isConnected())
session.disconnect();
}// end finally
}// end Descargar
public static void ListaDoc(String pUser, String pPass,String pHost, int pPort, String remotepath)
throws Exception {
JSch sftp = new JSch();
// Instancio el objeto session para la transferencia
Session session = null;
// instancio el canal sftp
ChannelSftp channelSftp = null;
try {
// Inciciamos el JSch con el usuario, host y puerto
session = sftp.getSession(pUser, pHost, pPort);
// Seteamos el password
session.setPassword(pPass);
// El SFTP requiere un intercambio de claves
// con esta propiedad le decimos que acepte la clave
// sin pedir confirmación
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// Abrimos el canal de sftp y conectamos
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.ls(remotepath);
} catch (SftpException e) {
throw new Exception(e);
} catch (JSchException e) {
throw new Exception(e);
} finally {
// Cerramos el canal y session
if (channelSftp.isConnected())
channelSftp.disconnect();
if (session.isConnected())
session.disconnect();
}// end finally
}// end ListaDoc
public static void main(String[] args) throws Exception{
Descargar(USERNAME, PASSWORD, HOST, PORT, LOCALPATH, REMOTEPATH,FILE);
} // End main
}