Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static final String USERNAME = "SFTP";
- static String HOST = "200.200.222.7";
- static int PORT = 55507;
- static String PASSWORD = "patitossecreto";
- static String REMOTEPATH ="";/*Agregar el working file para la descarga*/
- static String LOCALPATH="";/*Agregar el working file para la descarga*/
- static String FILE="";/*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();
- // 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 | SftpException | FileNotFoundException e) {
- throw new Exception(e);
- } finally {
- // Cerramos el canal y session
- if (channelSftp.isConnected())
- channelSftp.disconnect();
- if (session.isConnected())
- session.disconnect();
- }// end try
- }// 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 (JSchException | SftpException e) {
- throw new Exception(e);
- } finally {
- // Cerramos el canal y session
- if (channelSftp.isConnected())
- channelSftp.disconnect();
- if (session.isConnected())
- session.disconnect();
- }// end try
- }// end ListaDoc
- public static void main(String[] args) throws Exception{
- ListaDoc(USERNAME,PASSWORD, HOST, PORT, REMOTEPATH);
- Descargar(USERNAME, PASSWORD, HOST, PORT, LOCALPATH, REMOTEPATH,FILE);
- } // End main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement