1   package org.wcb.model.service.impl;
2   
3   import org.apache.commons.net.ftp.FTPClient;
4   import org.apache.commons.net.ftp.FTPReply;
5   import org.apache.commons.net.ftp.FTP;
6   import org.wcb.model.service.IFTPClientService;
7   
8   import java.io.*;
9   import java.net.SocketException;
10  
11  /**
12   * <small>
13   * Copyright (c)  2006  wbogaardt.
14   * Permission is granted to copy, distribute and/or modify this document
15   * under the terms of the GNU Free Documentation License, Version 1.2
16   * or any later version published by the Free Software Foundation;
17   * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
18   * Texts.  A copy of the license is included in the section entitled "GNU
19   * Free Documentation License".
20   * <p/>
21   * $File:  $ <br>
22   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 24, 2006 1:19:38 PM $ <br>
23   * </small>
24   *
25   * @author wbogaardt
26   * @version 1
27   *          Date: Mar 24, 2006
28   *          Time: 1:19:38 PM
29   */
30  public class FTPClientService implements IFTPClientService {
31  
32      private String hostAddress;
33      private int hostPort;
34      private String userName;
35      private String password;
36      private FTPClient client;
37  
38      public String getHostAddress() {
39          return hostAddress;
40      }
41  
42      public void setHostAddress(String hostAddress) {
43          this.hostAddress = hostAddress;
44      }
45  
46      public int getHostPort() {
47          if (hostPort == -1) {
48              hostPort = FTPClient.DEFAULT_PORT;
49          }
50          return hostPort;
51      }
52  
53      public void setHostPort(int hostPort) {
54          this.hostPort = hostPort;
55      }
56  
57      public String getUserName() {
58          return userName;
59      }
60  
61      public void setUserName(String userName) {
62          this.userName = userName;
63      }
64  
65      public String getPassword() {
66          return password;
67      }
68  
69      public void setPassword(String password) {
70          this.password = password;
71      }
72  
73      public void getFile(String remoteFileName, File localFile) {
74          if (client == null || !client.isConnected()) {
75              this.openConnection();
76          }
77          try {
78              OutputStream output = new BufferedOutputStream(new FileOutputStream(localFile));
79  
80              String directory = remoteFileName.substring(0, remoteFileName.lastIndexOf("/") + 1);
81              System.out.println("Directory CD: " + directory);
82              client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
83              client.setFileType(FTP.BINARY_FILE_TYPE);
84              client.changeWorkingDirectory(directory);
85              System.out.println(client.printWorkingDirectory());
86              System.out.println("Files in this directory:\n" + client.listNames());
87  
88              String fileNameRemote = remoteFileName.substring(remoteFileName.lastIndexOf("/") + 1, remoteFileName.length());
89                  System.out.println("Remote Filename is: " + fileNameRemote);
90              //InputStream input = client.retrieveFileStream(fileNameRemote);
91                //     Util.copyStream(input, output);
92                 client.retrieveFile(fileNameRemote, output);
93  
94              output.close();
95          } catch (FileNotFoundException fnfe) {
96              fnfe.printStackTrace();
97              this.closeConnection();
98          }
99          catch (IOException ioe) {
100             ioe.printStackTrace();
101             this.closeConnection();
102         }
103     }
104 
105     /**
106      * This allows puting a file from the local file system to the remote file system.
107      * @param localFile  Local file name and location
108      * @param remoteFileName  filename
109      * @param binaryTransfer  true indicates binary mode default is false and sends ascii.
110      */
111     public void putFile(File localFile, String remoteFileName, boolean binaryTransfer) {
112         if (client == null || !client.isConnected()) {
113             this.openConnection();
114         }
115         try {
116             System.out.println("Remote system is " + client.getSystemName());
117             if (binaryTransfer) {
118                 client.setFileType(FTP.BINARY_FILE_TYPE);
119             }
120             // Use passive mode as default because most of us are
121             // behind firewalls these days.
122             client.enterLocalPassiveMode();
123 
124             InputStream input;
125 
126             input = new FileInputStream(localFile);
127 
128             client.storeFile(remoteFileName, input);
129 
130             input.close();
131         } catch (IOException ioe) {
132             this.closeConnection();
133         }
134     }
135 
136     protected void closeConnection() {
137         if (client != null && client.isConnected()) {
138             try
139             {
140                 if(client.logout()) {
141                     client.disconnect();
142                 }
143             }
144             catch (IOException f)
145             {
146                 // do nothing
147             }
148         }
149 
150     }
151 
152     protected void openConnection() {
153         client = new FTPClient();
154         try {
155             client.connect(this.hostAddress);
156             System.out.println("Connected to " + this.hostAddress + ".");
157             // After connection attempt, you should check the reply code to verify
158             // success.
159             int reply = client.getReplyCode();
160 
161             if (!FTPReply.isPositiveCompletion(reply))
162             {
163                 this.closeConnection();
164             }
165         } catch (SocketException soe) {
166             soe.printStackTrace();
167         } catch (IOException ioe) {
168             if (client.isConnected())
169             {
170                 try
171                 {
172                     client.disconnect();
173                 }
174                 catch (IOException f)
175                 {
176                     // do nothing
177                 }
178             }
179             System.err.println("Could not connect to server.");
180             ioe.printStackTrace();
181         }
182         if (this.logonUser()) {
183             System.out.println("Logged on");
184         } else {
185             System.out.println("Not Logged");
186         }
187     }
188 
189     protected boolean logonUser() {
190         try {
191             if (this.userName != null && this.password != null) {
192                 if (!client.login(this.userName, this.password))
193                 {
194                     this.closeConnection();
195                     return false;
196                 }
197                 return true;
198             }
199         } catch (IOException e) {
200             return false;
201         }
202         return false;
203     }
204 }