001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package examples; 017 018 import java.io.IOException; 019 import java.io.PrintWriter; 020 import java.net.InetAddress; 021 import org.apache.commons.net.ProtocolCommandListener; 022 import org.apache.commons.net.ftp.FTPClient; 023 import org.apache.commons.net.ftp.FTPReply; 024 025 /*** 026 * This is an example program demonstrating how to use the FTPClient class. 027 * This program arranges a server to server file transfer that transfers 028 * a file from host1 to host2. Keep in mind, this program might only work 029 * if host2 is the same as the host you run it on (for security reasons, 030 * some ftp servers only allow PORT commands to be issued with a host 031 * argument equal to the client host). 032 * <p> 033 * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2> 034 * <p> 035 ***/ 036 public final class server2serverFTP 037 { 038 039 public static final void main(String[] args) 040 { 041 String server1, username1, password1, file1; 042 String server2, username2, password2, file2; 043 FTPClient ftp1, ftp2; 044 ProtocolCommandListener listener; 045 046 if (args.length < 8) 047 { 048 System.err.println( 049 "Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>" 050 ); 051 System.exit(1); 052 } 053 054 server1 = args[0]; 055 username1 = args[1]; 056 password1 = args[2]; 057 file1 = args[3]; 058 server2 = args[4]; 059 username2 = args[5]; 060 password2 = args[6]; 061 file2 = args[7]; 062 063 listener = new PrintCommandListener(new PrintWriter(System.out)); 064 ftp1 = new FTPClient(); 065 ftp1.addProtocolCommandListener(listener); 066 ftp2 = new FTPClient(); 067 ftp2.addProtocolCommandListener(listener); 068 069 try 070 { 071 int reply; 072 ftp1.connect(server1); 073 System.out.println("Connected to " + server1 + "."); 074 075 reply = ftp1.getReplyCode(); 076 077 if (!FTPReply.isPositiveCompletion(reply)) 078 { 079 ftp1.disconnect(); 080 System.err.println("FTP server1 refused connection."); 081 System.exit(1); 082 } 083 } 084 catch (IOException e) 085 { 086 if (ftp1.isConnected()) 087 { 088 try 089 { 090 ftp1.disconnect(); 091 } 092 catch (IOException f) 093 { 094 // do nothing 095 } 096 } 097 System.err.println("Could not connect to server1."); 098 e.printStackTrace(); 099 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 e) 118 { 119 if (ftp2.isConnected()) 120 { 121 try 122 { 123 ftp2.disconnect(); 124 } 125 catch (IOException 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 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 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 e) 207 { 208 // do nothing 209 } 210 } 211 } 212 }