KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.net.InetAddress JavaDoc;
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 public final class server2serverFTP
37 {
38
39     public static final void main(String JavaDoc[] args)
40     {
41         String JavaDoc server1, username1, password1, file1;
42         String JavaDoc server2, username2, password2, file2;
43         FTPClient ftp1, ftp2;
44         ProtocolCommandListener listener;
45
46         if (args.length < 8)
47         {
48             System.err.println(
49                 "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
50             );
51             System.exit(1);
52         }
53
54         server1 = args[0];
55         username1 = args[1];
56         password1 = args[2];
57         file1 = args[3];
58         server2 = args[4];
59         username2 = args[5];
60         password2 = args[6];
61         file2 = args[7];
62
63         listener = new PrintCommandListener(new PrintWriter JavaDoc(System.out));
64         ftp1 = new FTPClient();
65         ftp1.addProtocolCommandListener(listener);
66         ftp2 = new FTPClient();
67         ftp2.addProtocolCommandListener(listener);
68
69         try
70         {
71             int reply;
72             ftp1.connect(server1);
73             System.out.println("Connected to " + server1 + ".");
74
75             reply = ftp1.getReplyCode();
76
77             if (!FTPReply.isPositiveCompletion(reply))
78             {
79                 ftp1.disconnect();
80                 System.err.println("FTP server1 refused connection.");
81                 System.exit(1);
82             }
83         }
84         catch (IOException JavaDoc e)
85         {
86             if (ftp1.isConnected())
87             {
88                 try
89                 {
90                     ftp1.disconnect();
91                 }
92                 catch (IOException JavaDoc f)
93                 {
94                     // do nothing
95
}
96             }
97             System.err.println("Could not connect to server1.");
98             e.printStackTrace();
99             System.exit(1);
100         }
101
102         try
103         {
104             int reply;
105             ftp2.connect(server2);
106             System.out.println("Connected to " + server2 + ".");
107
108             reply = ftp2.getReplyCode();
109
110             if (!FTPReply.isPositiveCompletion(reply))
111             {
112                 ftp2.disconnect();
113                 System.err.println("FTP server2 refused connection.");
114                 System.exit(1);
115             }
116         }
117         catch (IOException JavaDoc e)
118         {
119             if (ftp2.isConnected())
120             {
121                 try
122                 {
123                     ftp2.disconnect();
124                 }
125                 catch (IOException JavaDoc f)
126                 {
127                     // do nothing
128
}
129             }
130             System.err.println("Could not connect to server2.");
131             e.printStackTrace();
132             System.exit(1);
133         }
134
135 __main:
136         try
137         {
138             if (!ftp1.login(username1, password1))
139             {
140                 System.err.println("Could not login to " + server1);
141                 break __main;
142             }
143
144             if (!ftp2.login(username2, password2))
145             {
146                 System.err.println("Could not login to " + server2);
147                 break __main;
148             }
149
150             // Let's just assume success for now.
151
ftp2.enterRemotePassiveMode();
152
153             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
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
ftp1.completePendingCommand();
168                 ftp2.completePendingCommand();
169             }
170             else
171             {
172                 System.err.println(
173                     "Couldn't initiate transfer. Check that filenames are valid.");
174                 break __main;
175             }
176
177         }
178         catch (IOException JavaDoc e)
179         {
180             e.printStackTrace();
181             System.exit(1);
182         }
183         finally
184         {
185             try
186             {
187                 if (ftp1.isConnected())
188                 {
189                     ftp1.logout();
190                     ftp1.disconnect();
191                 }
192             }
193             catch (IOException JavaDoc e)
194             {
195                 // do nothing
196
}
197
198             try
199             {
200                 if (ftp2.isConnected())
201                 {
202                     ftp2.logout();
203                     ftp2.disconnect();
204                 }
205             }
206             catch (IOException JavaDoc e)
207             {
208                 // do nothing
209
}
210         }
211     }
212 }
213
Popular Tags