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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
91
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
107
108
109
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
121
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
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
158
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
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 }