001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package examples;
019
020 import java.io.IOException;
021 import java.io.PrintWriter;
022 import java.net.InetAddress;
023
024 import org.apache.commons.net.PrintCommandListener;
025 import org.apache.commons.net.ProtocolCommandListener;
026 import org.apache.commons.net.ftp.FTPClient;
027 import org.apache.commons.net.ftp.FTPReply;
028
029 /***
030 * This is an example program demonstrating how to use the FTPClient class.
031 * This program arranges a server to server file transfer that transfers
032 * a file from host1 to host2. Keep in mind, this program might only work
033 * if host2 is the same as the host you run it on (for security reasons,
034 * some ftp servers only allow PORT commands to be issued with a host
035 * argument equal to the client host).
036 * <p>
037 * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>
038 * <p>
039 ***/
040 public final class server2serverFTP
041 {
042
043 public static final void main(String[] args)
044 {
045 String server1, username1, password1, file1;
046 String server2, username2, password2, file2;
047 FTPClient ftp1, ftp2;
048 ProtocolCommandListener listener;
049
050 if (args.length < 8)
051 {
052 System.err.println(
053 "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>"
054 );
055 System.exit(1);
056 }
057
058 server1 = args[0];
059 username1 = args[1];
060 password1 = args[2];
061 file1 = args[3];
062 server2 = args[4];
063 username2 = args[5];
064 password2 = args[6];
065 file2 = args[7];
066
067 listener = new PrintCommandListener(new PrintWriter(System.out));
068 ftp1 = new FTPClient();
069 ftp1.addProtocolCommandListener(listener);
070 ftp2 = new FTPClient();
071 ftp2.addProtocolCommandListener(listener);
072
073 try
074 {
075 int reply;
076 ftp1.connect(server1);
077 System.out.println("Connected to " + server1 + ".");
078
079 reply = ftp1.getReplyCode();
080
081 if (!FTPReply.isPositiveCompletion(reply))
082 {
083 ftp1.disconnect();
084 System.err.println("FTP server1 refused connection.");
085 System.exit(1);
086 }
087 }
088 catch (IOException e)
089 {
090 if (ftp1.isConnected())
091 {
092 try
093 {
094 ftp1.disconnect();
095 }
096 catch (IOException f)
097 {
098 // do nothing
099 }
100 }
101 System.err.println("Could not connect to server1.");
102 e.printStackTrace();
103 System.exit(1);
104 }
105
106 try
107 {
108 int reply;
109 ftp2.connect(server2);
110 System.out.println("Connected to " + server2 + ".");
111
112 reply = ftp2.getReplyCode();
113
114 if (!FTPReply.isPositiveCompletion(reply))
115 {
116 ftp2.disconnect();
117 System.err.println("FTP server2 refused connection.");
118 System.exit(1);
119 }
120 }
121 catch (IOException e)
122 {
123 if (ftp2.isConnected())
124 {
125 try
126 {
127 ftp2.disconnect();
128 }
129 catch (IOException f)
130 {
131 // do nothing
132 }
133 }
134 System.err.println("Could not connect to server2.");
135 e.printStackTrace();
136 System.exit(1);
137 }
138
139 __main:
140 try
141 {
142 if (!ftp1.login(username1, password1))
143 {
144 System.err.println("Could not login to " + server1);
145 break __main;
146 }
147
148 if (!ftp2.login(username2, password2))
149 {
150 System.err.println("Could not login to " + server2);
151 break __main;
152 }
153
154 // Let's just assume success for now.
155 ftp2.enterRemotePassiveMode();
156
157 ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
158 ftp2.getPassivePort());
159
160 // Although you would think the store command should be sent to server2
161 // first, in reality, ftp servers like wu-ftpd start accepting data
162 // connections right after entering passive mode. Additionally, they
163 // don't even send the positive preliminary reply until after the
164 // transfer is completed (in the case of passive mode transfers).
165 // Therefore, calling store first would hang waiting for a preliminary
166 // reply.
167 if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
168 {
169 // if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
170 // We have to fetch the positive completion reply.
171 ftp1.completePendingCommand();
172 ftp2.completePendingCommand();
173 }
174 else
175 {
176 System.err.println(
177 "Couldn't initiate transfer. Check that filenames are valid.");
178 break __main;
179 }
180
181 }
182 catch (IOException e)
183 {
184 e.printStackTrace();
185 System.exit(1);
186 }
187 finally
188 {
189 try
190 {
191 if (ftp1.isConnected())
192 {
193 ftp1.logout();
194 ftp1.disconnect();
195 }
196 }
197 catch (IOException e)
198 {
199 // do nothing
200 }
201
202 try
203 {
204 if (ftp2.isConnected())
205 {
206 ftp2.logout();
207 ftp2.disconnect();
208 }
209 }
210 catch (IOException e)
211 {
212 // do nothing
213 }
214 }
215 }
216 }