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: public static final void main(String[] args) {
039: String server1, username1, password1, file1;
040: String server2, username2, password2, file2;
041: FTPClient ftp1, ftp2;
042: ProtocolCommandListener listener;
043:
044: if (args.length < 8) {
045: System.err
046: .println("Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2> <pass2> <file2>");
047: System.exit(1);
048: }
049:
050: server1 = args[0];
051: username1 = args[1];
052: password1 = args[2];
053: file1 = args[3];
054: server2 = args[4];
055: username2 = args[5];
056: password2 = args[6];
057: file2 = args[7];
058:
059: listener = new PrintCommandListener(new PrintWriter(System.out));
060: ftp1 = new FTPClient();
061: ftp1.addProtocolCommandListener(listener);
062: ftp2 = new FTPClient();
063: ftp2.addProtocolCommandListener(listener);
064:
065: try {
066: int reply;
067: ftp1.connect(server1);
068: System.out.println("Connected to " + server1 + ".");
069:
070: reply = ftp1.getReplyCode();
071:
072: if (!FTPReply.isPositiveCompletion(reply)) {
073: ftp1.disconnect();
074: System.err.println("FTP server1 refused connection.");
075: System.exit(1);
076: }
077: } catch (IOException e) {
078: if (ftp1.isConnected()) {
079: try {
080: ftp1.disconnect();
081: } catch (IOException f) {
082: // do nothing
083: }
084: }
085: System.err.println("Could not connect to server1.");
086: e.printStackTrace();
087: System.exit(1);
088: }
089:
090: try {
091: int reply;
092: ftp2.connect(server2);
093: System.out.println("Connected to " + server2 + ".");
094:
095: reply = ftp2.getReplyCode();
096:
097: if (!FTPReply.isPositiveCompletion(reply)) {
098: ftp2.disconnect();
099: System.err.println("FTP server2 refused connection.");
100: System.exit(1);
101: }
102: } catch (IOException e) {
103: if (ftp2.isConnected()) {
104: try {
105: ftp2.disconnect();
106: } catch (IOException f) {
107: // do nothing
108: }
109: }
110: System.err.println("Could not connect to server2.");
111: e.printStackTrace();
112: System.exit(1);
113: }
114:
115: __main: try {
116: if (!ftp1.login(username1, password1)) {
117: System.err.println("Could not login to " + server1);
118: break __main;
119: }
120:
121: if (!ftp2.login(username2, password2)) {
122: System.err.println("Could not login to " + server2);
123: break __main;
124: }
125:
126: // Let's just assume success for now.
127: ftp2.enterRemotePassiveMode();
128:
129: ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2
130: .getPassiveHost()), ftp2.getPassivePort());
131:
132: // Although you would think the store command should be sent to server2
133: // first, in reality, ftp servers like wu-ftpd start accepting data
134: // connections right after entering passive mode. Additionally, they
135: // don't even send the positive preliminary reply until after the
136: // transfer is completed (in the case of passive mode transfers).
137: // Therefore, calling store first would hang waiting for a preliminary
138: // reply.
139: if (ftp1.remoteRetrieve(file1)
140: && ftp2.remoteStoreUnique(file2)) {
141: // if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2)) {
142: // We have to fetch the positive completion reply.
143: ftp1.completePendingCommand();
144: ftp2.completePendingCommand();
145: } else {
146: System.err
147: .println("Couldn't initiate transfer. Check that filenames are valid.");
148: break __main;
149: }
150:
151: } catch (IOException e) {
152: e.printStackTrace();
153: System.exit(1);
154: } finally {
155: try {
156: if (ftp1.isConnected()) {
157: ftp1.logout();
158: ftp1.disconnect();
159: }
160: } catch (IOException e) {
161: // do nothing
162: }
163:
164: try {
165: if (ftp2.isConnected()) {
166: ftp2.logout();
167: ftp2.disconnect();
168: }
169: } catch (IOException e) {
170: // do nothing
171: }
172: }
173: }
174: }
|