summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java')
-rw-r--r--src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java
new file mode 100644
index 0000000..d83c0ea
--- /dev/null
+++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtil.java
@@ -0,0 +1,88 @@
+package org.openslx.satellitedaemon.ftp;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import org.apache.commons.net.ftp.FTPClient;
+
+public class FtpUpDownUtil {
+ /**
+ * FTP-Dateienliste.
+ * @return String-Array der Dateinamen auf dem FTP-Server
+ */
+ public static String[] list( String host, int port, String usr, String pwd ) throws IOException
+ {
+ FTPClient ftpClient = new FTPClient();
+ String[] filenameList;
+
+ try {
+ ftpClient.connect( host, port );
+ ftpClient.login( usr, pwd );
+ filenameList = ftpClient.listNames();
+ ftpClient.logout();
+ } finally {
+ ftpClient.disconnect();
+ }
+
+ return filenameList;
+ }
+
+ /**
+ * FTP-Client-Download.
+ * @return true falls ok
+ */
+ public static boolean download( String localResultFile, String remoteSourceFile,
+ String host, int port, String usr, String pwd, boolean showMessages ) throws IOException
+ {
+ FTPClient ftpClient = new FTPClient();
+ FileOutputStream fos = null;
+ boolean resultOk = true;
+
+ try {
+ ftpClient.connect( host, port );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ resultOk &= ftpClient.login( usr, pwd );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ fos = new FileOutputStream( localResultFile );
+ resultOk &= ftpClient.retrieveFile( remoteSourceFile, fos );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ resultOk &= ftpClient.logout();
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ } finally {
+ try { if( fos != null ) { fos.close(); } } catch( IOException e ) {/* nothing to do */}
+ ftpClient.disconnect();
+ }
+
+ return resultOk;
+ }
+
+ /**
+ * FTP-Client-Upload.
+ * @return true falls ok
+ */
+ public static boolean upload( String localSourceFile, String remoteResultFile,
+ String host, int port, String usr, String pwd, boolean showMessages ) throws IOException
+ {
+ FTPClient ftpClient = new FTPClient();
+ FileInputStream fis = null;
+ boolean resultOk = true;
+
+ try {
+ ftpClient.connect( host, port );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ resultOk &= ftpClient.login( usr, pwd );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ fis = new FileInputStream( localSourceFile );
+ resultOk &= ftpClient.storeFile( remoteResultFile, fis );
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ resultOk &= ftpClient.logout();
+ if( showMessages ) { System.out.println( ftpClient.getReplyString() ); }
+ } finally {
+ try { if( fis != null ) { fis.close(); } } catch( IOException e ) {/* nothing to do */}
+ ftpClient.disconnect();
+ }
+
+ return resultOk;
+ }
+}