Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package de.xxxxxx.xmlparser.libs;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Hashtable;
  5.  
  6. import com.jcraft.jsch.*;
  7. import org.apache.log4j.Logger;
  8.  
  9. /**
  10.  * Eine Klasse zum Auslesen von Dateien über SFTP per InputStream
  11.  *
  12.  * @author Patrick Trautmann
  13.  * @version 1.0, 03.08.2010
  14.  */
  15. public class SftpPull {
  16.  
  17.     private static Logger logger = Logger.getLogger( SftpPull.class );
  18.    
  19.     //Variablen für den SFTP Zugriff
  20.     private JSch sftpPool = null;
  21.     private ChannelSftp sftpChannel = null;
  22.     private Session sftpSession = null;
  23.    
  24.     //SFTP Connection Defaults
  25.     private String sftpHostname = "";
  26.     private String sftpUsername = "";
  27.     private String sftpPassword = "";
  28.     private int sftpPort = 22;
  29.    
  30.     private InputStream result = null;
  31.    
  32.     /**
  33.      * Konstruktor der Klasse.
  34.      * @param Hostname
  35.      * @param Username
  36.      * @param Password
  37.      */
  38.     public SftpPull(String Hostname, String Username, String Password) {
  39.         this.sftpHostname = Hostname;
  40.         this.sftpUsername = Username;
  41.         this.sftpPassword = Password;
  42.     }
  43.    
  44.     /**
  45.      * Konstruktor der Klasse.
  46.      *
  47.      * @param Hostname
  48.      * @param Username
  49.      * @param Password
  50.      * @param Port
  51.      */
  52.     public SftpPull(String Hostname, String Username, String Password, int Port) {
  53.         this.sftpHostname = Hostname;
  54.         this.sftpUsername = Username;
  55.         this.sftpPassword = Password;
  56.         this.sftpPort = Port;
  57.     }
  58.    
  59.     /**
  60.      * Initialisieren der SFTP Variablen um eine erfolgereiche Verbindung herstellen zu können.
  61.      *
  62.      * @throws JSchException    Wird bei SFTP Problemen ausgelöst
  63.      */
  64.     @SuppressWarnings("unchecked")
  65.     public void init_SFTP() {
  66.         this.sftpPool = new JSch();
  67.         try {
  68.             //Verbindung herstellen
  69.             sftpSession = this.sftpPool.getSession(this.sftpUsername, this.sftpHostname, this.sftpPort);
  70.            
  71.             //Config aufbauen
  72.             @SuppressWarnings("rawtypes")
  73.             Hashtable config = new Hashtable();
  74.             config.put("StrictHostKeyChecking", "no");
  75.             sftpSession.setConfig(config);
  76.             sftpSession.setPassword(this.sftpPassword);
  77.            
  78.             //Verbindung aufbauen
  79.             sftpSession.connect();
  80.            
  81.             sftpChannel = (ChannelSftp) sftpSession.openChannel("sftp");
  82.             sftpChannel.connect();
  83.            
  84.             //Logger
  85.             logger.debug("SFTP Verbidnungs wurden ausgeführt.");
  86.            
  87.         }
  88.         catch(JSchException e) {
  89.             logger.error(e.toString());
  90.         }
  91.  
  92.     }
  93.    
  94.     /**
  95.      * Liest die XML Datei per SFTP aus und gibt sie als InputStream wieder.
  96.      *
  97.      * @param filepath          Der Dateipfad auf dem Unixsystem zur XML Datei
  98.      * @param filename          Der Name der XML Datei
  99.      * @throws SftpException    Fehler die beim Areiten mit SFTP auftreten
  100.      * @return InputStream      Die XML Datei des gesuchten Dateipfades
  101.      */
  102.     public InputStream readOut(String filepath, String filename) throws SftpException {
  103.        
  104.         if(this.sftpChannel.isConnected()) {
  105.             logger.debug("Die Verbindung zum SFTP Server wurde erfolgreich hergestellt.");
  106.             sftpChannel.cd(filepath);
  107.             this.result = sftpChannel.get(filename);
  108.         }
  109.        
  110.         logger.debug("InputStream wurde zurückgegeben");
  111.         return this.result;
  112.        
  113.     }
  114.    
  115.     /**
  116.      * SFTP Verbindung und InputStream schließen
  117.      *
  118.      * @throws IOException  Fehler die beim schließen des InputStreams auftreten
  119.      */
  120.     public void close_SFTP() {
  121.    
  122.         try {
  123.             this.sftpSession.disconnect();
  124.             logger.debug("SFTP Verbindung wurde geschlossen.");
  125.             this.result.close();
  126.             logger.debug("Der InputStream wurde geschlossen");
  127.         }
  128.         catch(IOException e) {
  129.             logger.error(e.toString());
  130.         }
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement