|
| 1 | +package FTPClient; |
| 2 | + |
| 3 | +import org.apache.commons.net.ftp.FTPClient; |
| 4 | +import org.apache.commons.net.ftp.FTPFile; |
| 5 | +import org.apache.commons.net.ftp.FTPReply; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.net.SocketException; |
| 9 | +import java.text.SimpleDateFormat; |
| 10 | +import java.util.Date; |
| 11 | + |
| 12 | +public class FtpClientImpl extends Thread implements FTPInterface{ |
| 13 | + |
| 14 | + public FtpClientImpl(OutputStream os) { |
| 15 | + logStream = new PrintStream(os); |
| 16 | + } |
| 17 | + |
| 18 | + public FtpClientImpl() { |
| 19 | + LOGGING_OVER_FILE = false; |
| 20 | + } |
| 21 | + |
| 22 | + private FTPClient client; |
| 23 | + |
| 24 | + private PrintStream logStream; |
| 25 | + private String LOG_TAG = "FTP Client"; |
| 26 | + private boolean LOGGING_OVER_FILE = true; |
| 27 | + |
| 28 | + @Override |
| 29 | + 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; |
| 63 | + } |
| 64 | + } |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean isConnected() { |
| 70 | + return client.isConnected(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public FTPFile[] getFileList() throws IOException { |
| 75 | + if(client == null) { |
| 76 | + System.out.println("INFO : First initialize the FTPClient by calling 'initFTPPassiveClient()'"); |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + log("DEBUG : Getting file listing for current director"); |
| 81 | + FTPFile[] files = client.listFiles(""); |
| 82 | + return files; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void downloadFile(String remoteFileName, String downloadPath) throws IOException { |
| 87 | + // Download File using retrieveFile(String, OutputStream) |
| 88 | + OutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(new File(downloadPath))); |
| 89 | + boolean success = client.retrieveFile(remoteFileName, fileOutputStream); |
| 90 | + fileOutputStream.close(); |
| 91 | + |
| 92 | + if (success) |
| 93 | + log("INFO : File " + remoteFileName + " has been downloaded successfully."); |
| 94 | + else |
| 95 | + log("ERROR : couldn't download " + remoteFileName + "from server!"); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void closeConnection() { |
| 101 | + if( client == null ) { |
| 102 | + log("Nothing to close, the FTPClient wasn't initialized"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + //be polite and logout & close the connection before the application finishes |
| 107 | + try { |
| 108 | + client.logout(); |
| 109 | + client.disconnect(); |
| 110 | + } catch (IOException e) { |
| 111 | + String message = "Could not logout"; |
| 112 | + log(message+"\n"); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void log(String msg) { |
| 117 | + |
| 118 | + if (!LOGGING_OVER_FILE) { |
| 119 | + SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); |
| 120 | + Date date = new Date(System.currentTimeMillis()); |
| 121 | + System.out.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (logStream == null) { |
| 126 | + throw new NullPointerException("ERROR: PrintStream for webServiceLog not initialized"); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); |
| 131 | + Date date = new Date(System.currentTimeMillis()); |
| 132 | + logStream.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | +} |
0 commit comments