1- package FTPClient ;
2-
31import org .apache .commons .net .ftp .FTPClient ;
42import org .apache .commons .net .ftp .FTPFile ;
53import org .apache .commons .net .ftp .FTPReply ;
64
75import java .io .*;
86import java .net .SocketException ;
97import java .text .SimpleDateFormat ;
8+ import java .util .ArrayList ;
109import java .util .Date ;
1110
12- public class FtpClientImpl extends Thread implements FTPInterface {
11+ public class FtpClient extends Thread implements FTPInterface {
1312
14- public FtpClientImpl (OutputStream os ) {
13+ public FtpClient (OutputStream os ) {
1514 logStream = new PrintStream (os );
1615 }
1716
18- public FtpClientImpl () {
17+ public FtpClient () {
1918 LOGGING_OVER_FILE = false ;
2019 }
2120
@@ -27,42 +26,42 @@ public FtpClientImpl() {
2726
2827 @ Override
2928 public int connect (String server , int port , String user , String pass ) {
30- if ( client == null ) {
31- log ("Getting passive FTP client" );
32- client = new FTPClient ();
33-
34- try {
35-
36- client .connect (server , port );
37- // After connection attempt, you should check the reply code to verify
38- // success.
39- int reply = client .getReplyCode ();
40-
41- if (!FTPReply .isPositiveCompletion (reply )) {
42- client .disconnect ();
43- log (" ERROR : FTP server refused connection." );
44- return 1 ;
45- }
46-
47- //after connecting to the server set the local passive mode
48- client .enterLocalPassiveMode ();
49-
50- //send username and password to login to the server
51- if ( !client .login (user , pass ) ) {
52- log (" ERROR - Could not login to FTP Server" );
53- return 2 ;
54- }
55- } catch (SocketException e ) {
56- String message = "Socket exception thrown, Server not found" ;
57- log ("ERROR :" + message +"\n " + e );
58- return 3 ;
59- } catch (IOException e ) {
60- String message = "IO Exception" ;
61- log ("ERROR :" + message +"\n " + e );
62- return 4 ;
29+ if ( client == null ) {
30+ log ("Getting passive FTP client" );
31+ client = new FTPClient ();
32+
33+ try {
34+
35+ client .connect (server , port );
36+ // After connection attempt, you should check the reply code to verify
37+ // success.
38+ int reply = client .getReplyCode ();
39+
40+ if (!FTPReply .isPositiveCompletion (reply )) {
41+ client .disconnect ();
42+ log (" ERROR : FTP server refused connection." );
43+ return 1 ;
6344 }
45+
46+ //after connecting to the server set the local passive mode
47+ client .enterLocalPassiveMode ();
48+
49+ //send username and password to login to the server
50+ if ( !client .login (user , pass ) ) {
51+ log (" ERROR - Could not login to FTP Server" );
52+ return 2 ;
53+ }
54+ } catch (SocketException e ) {
55+ String message = "Socket exception thrown, Server not found" ;
56+ log ("ERROR :" + message +"\n " + e );
57+ return 3 ;
58+ } catch (IOException e ) {
59+ String message = "IO Exception" ;
60+ log ("ERROR :" + message +"\n " + e );
61+ return 4 ;
6462 }
65- return 0 ;
63+ }
64+ return 0 ;
6665 }
6766
6867 @ Override
@@ -71,15 +70,39 @@ public boolean isConnected() {
7170 }
7271
7372 @ Override
74- public FTPFile [] getFileList () throws IOException {
73+ public FTPFile [] getDirList (String path ) throws IOException {
74+ if (client == null ) {
75+ System .out .println ("INFO : First initialize the FTPClient by calling 'initFTPPassiveClient()'" );
76+ return null ;
77+ }
78+
79+ log ("DEBUG : Getting file listing for current director" );
80+ FTPFile [] files = client .listFiles (path );
81+ ArrayList <FTPFile > dirList = new ArrayList <>();
82+ for (FTPFile f :
83+ files ) {
84+ if (f .isDirectory ())
85+ dirList .add (f );
86+ }
87+ return dirList .toArray (new FTPFile [dirList .size ()]);
88+ }
89+
90+ @ Override
91+ public FTPFile [] getFileList (String path ) throws IOException {
7592 if (client == null ) {
7693 System .out .println ("INFO : First initialize the FTPClient by calling 'initFTPPassiveClient()'" );
7794 return null ;
7895 }
7996
8097 log ("DEBUG : Getting file listing for current director" );
81- FTPFile [] files = client .listFiles ("" );
82- return files ;
98+ FTPFile [] files = client .listFiles (path );
99+ ArrayList <FTPFile > actualFiles = new ArrayList <>();
100+ for (FTPFile f :
101+ files ) {
102+ if (!f .isDirectory ())
103+ actualFiles .add (f );
104+ }
105+ return actualFiles .toArray (new FTPFile [actualFiles .size ()]);
83106 }
84107
85108 @ Override
0 commit comments