Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. static final String USERNAME = "SFTP";
  2. static String HOST = "200.200.222.7";
  3. static int PORT = 55507;
  4. static String PASSWORD = "patitossecreto";
  5. static String REMOTEPATH ="";/*Agregar el working file para la descarga*/
  6. static String LOCALPATH="";/*Agregar el working file para la descarga*/
  7. static String FILE="";/*Archivo que deseamos descargar*/
  8.  
  9. public static void Descargar(String pUser, String pPass,String pHost, int pPort, String localpath, String remotepath, String file)
  10. throws Exception {
  11. JSch sftp = new JSch();
  12. // Instancio el objeto session para la transferencia
  13. Session session = null;
  14. // instancio el canal sftp
  15. ChannelSftp channelSftp = null;
  16. try {
  17. // Inciciamos el JSch con el usuario, host y puerto
  18. session = sftp.getSession(pUser, pHost, pPort);
  19. // Seteamos el password
  20. session.setPassword(pPass);
  21. // El SFTP requiere un intercambio de claves
  22. // con esta propiedad le decimos que acepte la clave
  23. // sin pedir confirmación
  24. Properties prop = new Properties();
  25. prop.put("StrictHostKeyChecking", "no");
  26. session.setConfig(prop);
  27. session.connect();
  28.  
  29. // Abrimos el canal de sftp y conectamos
  30. channelSftp = (ChannelSftp) session.openChannel("sftp");
  31. channelSftp.connect();
  32.  
  33. // Convertimos el archivo a transferir en un OutputStream donde se va a guardar
  34. OutputStream os = new BufferedOutputStream(new FileOutputStream(
  35. localpath+'/'+file));
  36. // Iniciamos la transferencia
  37. channelSftp.get(remotepath+'/'+file, os);
  38. } catch (JSchException | SftpException | FileNotFoundException e) {
  39. throw new Exception(e);
  40. } finally {
  41. // Cerramos el canal y session
  42. if (channelSftp.isConnected())
  43. channelSftp.disconnect();
  44. if (session.isConnected())
  45. session.disconnect();
  46. }// end try
  47. }// end Descargar
  48.  
  49. public static void ListaDoc(String pUser, String pPass,String pHost, int pPort, String remotepath)
  50. throws Exception {
  51. JSch sftp = new JSch();
  52. // Instancio el objeto session para la transferencia
  53. Session session = null;
  54. // instancio el canal sftp
  55. ChannelSftp channelSftp = null;
  56. try {
  57. // Inciciamos el JSch con el usuario, host y puerto
  58. session = sftp.getSession(pUser, pHost, pPort);
  59. // Seteamos el password
  60. session.setPassword(pPass);
  61. // El SFTP requiere un intercambio de claves
  62. // con esta propiedad le decimos que acepte la clave
  63. // sin pedir confirmación
  64. Properties prop = new Properties();
  65. prop.put("StrictHostKeyChecking", "no");
  66. session.setConfig(prop);
  67. session.connect();
  68.  
  69. // Abrimos el canal de sftp y conectamos
  70. channelSftp = (ChannelSftp) session.openChannel("sftp");
  71. channelSftp.connect();
  72.  
  73. channelSftp.ls(remotepath);
  74.  
  75. } catch (JSchException | SftpException e) {
  76. throw new Exception(e);
  77. } finally {
  78. // Cerramos el canal y session
  79. if (channelSftp.isConnected())
  80. channelSftp.disconnect();
  81. if (session.isConnected())
  82. session.disconnect();
  83. }// end try
  84. }// end ListaDoc
  85.  
  86. public static void main(String[] args) throws Exception{
  87. ListaDoc(USERNAME,PASSWORD, HOST, PORT, REMOTEPATH);
  88. Descargar(USERNAME, PASSWORD, HOST, PORT, LOCALPATH, REMOTEPATH,FILE);
  89. } // End main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement