1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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;
}
}
|