%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
examples.server2serverFTP |
|
|
1 | /* |
|
2 | * Copyright 2001-2005 The Apache Software Foundation |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package examples; |
|
17 | ||
18 | import java.io.IOException; |
|
19 | import java.io.PrintWriter; |
|
20 | import java.net.InetAddress; |
|
21 | import org.apache.commons.net.ProtocolCommandListener; |
|
22 | import org.apache.commons.net.ftp.FTPClient; |
|
23 | import org.apache.commons.net.ftp.FTPReply; |
|
24 | ||
25 | /*** |
|
26 | * This is an example program demonstrating how to use the FTPClient class. |
|
27 | * This program arranges a server to server file transfer that transfers |
|
28 | * a file from host1 to host2. Keep in mind, this program might only work |
|
29 | * if host2 is the same as the host you run it on (for security reasons, |
|
30 | * some ftp servers only allow PORT commands to be issued with a host |
|
31 | * argument equal to the client host). |
|
32 | * <p> |
|
33 | * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2> |
|
34 | * <p> |
|
35 | ***/ |
|
36 | 0 | public final class server2serverFTP |
37 | { |
|
38 | ||
39 | public static final void main(String[] args) |
|
40 | { |
|
41 | String server1, username1, password1, file1; |
|
42 | String server2, username2, password2, file2; |
|
43 | FTPClient ftp1, ftp2; |
|
44 | ProtocolCommandListener listener; |
|
45 | ||
46 | 0 | if (args.length < 8) |
47 | { |
|
48 | 0 | System.err.println( |
49 | "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>" |
|
50 | ); |
|
51 | 0 | System.exit(1); |
52 | } |
|
53 | ||
54 | 0 | server1 = args[0]; |
55 | 0 | username1 = args[1]; |
56 | 0 | password1 = args[2]; |
57 | 0 | file1 = args[3]; |
58 | 0 | server2 = args[4]; |
59 | 0 | username2 = args[5]; |
60 | 0 | password2 = args[6]; |
61 | 0 | file2 = args[7]; |
62 | ||
63 | 0 | listener = new PrintCommandListener(class="keyword">new PrintWriter(System.out)); |
64 | 0 | ftp1 = new FTPClient(); |
65 | 0 | ftp1.addProtocolCommandListener(listener); |
66 | 0 | ftp2 = new FTPClient(); |
67 | 0 | ftp2.addProtocolCommandListener(listener); |
68 | ||
69 | try |
|
70 | { |
|
71 | int reply; |
|
72 | 0 | ftp1.connect(server1); |
73 | 0 | System.out.println("Connected to " + server1 + "."); |
74 | ||
75 | 0 | reply = ftp1.getReplyCode(); |
76 | ||
77 | 0 | if (!FTPReply.isPositiveCompletion(reply)) |
78 | { |
|
79 | 0 | ftp1.disconnect(); |
80 | 0 | System.err.println("FTP server1 refused connection."); |
81 | 0 | System.exit(1); |
82 | } |
|
83 | } |
|
84 | 0 | catch (IOException e) |
85 | { |
|
86 | 0 | if (ftp1.isConnected()) |
87 | { |
|
88 | try |
|
89 | { |
|
90 | 0 | ftp1.disconnect(); |
91 | } |
|
92 | 0 | catch (IOException f) |
93 | { |
|
94 | // do nothing |
|
95 | 0 | } |
96 | } |
|
97 | 0 | System.err.println("Could not connect to server1."); |
98 | 0 | e.printStackTrace(); |
99 | 0 | System.exit(1); |
100 | 0 | } |
101 | ||
102 | try |
|
103 | { |
|
104 | int reply; |
|
105 | 0 | ftp2.connect(server2); |
106 | 0 | System.out.println("Connected to " + server2 + "."); |
107 | ||
108 | 0 | reply = ftp2.getReplyCode(); |
109 | ||
110 | 0 | if (!FTPReply.isPositiveCompletion(reply)) |
111 | { |
|
112 | 0 | ftp2.disconnect(); |
113 | 0 | System.err.println("FTP server2 refused connection."); |
114 | 0 | System.exit(1); |
115 | } |
|
116 | } |
|
117 | 0 | catch (IOException e) |
118 | { |
|
119 | 0 | if (ftp2.isConnected()) |
120 | { |
|
121 | try |
|
122 | { |
|
123 | 0 | ftp2.disconnect(); |
124 | } |
|
125 | 0 | catch (IOException f) |
126 | { |
|
127 | // do nothing |
|
128 | 0 | } |
129 | } |
|
130 | 0 | System.err.println("Could not connect to server2."); |
131 | 0 | e.printStackTrace(); |
132 | 0 | System.exit(1); |
133 | 0 | } |
134 | ||
135 | __main: |
|
136 | try |
|
137 | { |
|
138 | 0 | if (!ftp1.login(username1, password1)) |
139 | { |
|
140 | 0 | System.err.println("Could not login to " + server1); |
141 | 0 | break __main; |
142 | } |
|
143 | ||
144 | 0 | if (!ftp2.login(username2, password2)) |
145 | { |
|
146 | 0 | System.err.println("Could not login to " + server2); |
147 | 0 | break __main; |
148 | } |
|
149 | ||
150 | // Let's just assume success for now. |
|
151 | 0 | ftp2.enterRemotePassiveMode(); |
152 | ||
153 | 0 | ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()), |
154 | ftp2.getPassivePort()); |
|
155 | ||
156 | // Although you would think the store command should be sent to server2 |
|
157 | // first, in reality, ftp servers like wu-ftpd start accepting data |
|
158 | // connections right after entering passive mode. Additionally, they |
|
159 | // don't even send the positive preliminary reply until after the |
|
160 | // transfer is completed (in the case of passive mode transfers). |
|
161 | // Therefore, calling store first would hang waiting for a preliminary |
|
162 | // reply. |
|
163 | 0 | if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2)) |
164 | { |
|
165 | // if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) { |
|
166 | // We have to fetch the positive completion reply. |
|
167 | 0 | ftp1.completePendingCommand(); |
168 | 0 | ftp2.completePendingCommand(); |
169 | } |
|
170 | else |
|
171 | { |
|
172 | 0 | System.err.println( |
173 | "Couldn't initiate transfer. Check that filenames are valid."); |
|
174 | 0 | break __main; |
175 | } |
|
176 | ||
177 | 0 | } |
178 | 0 | catch (IOException e) |
179 | { |
|
180 | 0 | e.printStackTrace(); |
181 | 0 | System.exit(1); |
182 | 0 | } |
183 | finally |
|
184 | { |
|
185 | 0 | try |
186 | { |
|
187 | 0 | if (ftp1.isConnected()) |
188 | { |
|
189 | 0 | ftp1.logout(); |
190 | 0 | ftp1.disconnect(); |
191 | } |
|
192 | } |
|
193 | 0 | catch (IOException e) |
194 | { |
|
195 | // do nothing |
|
196 | 0 | } |
197 | ||
198 | try |
|
199 | { |
|
200 | 0 | if (ftp2.isConnected()) |
201 | { |
|
202 | 0 | ftp2.logout(); |
203 | 0 | ftp2.disconnect(); |
204 | } |
|
205 | } |
|
206 | 0 | catch (IOException e) |
207 | { |
|
208 | // do nothing |
|
209 | 0 | } |
210 | 0 | } |
211 | 0 | } |
212 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |