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.FileInputStream;
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.io.PrintWriter;
024: import org.apache.commons.net.ftp.FTP;
025: import org.apache.commons.net.ftp.FTPClient;
026: import org.apache.commons.net.ftp.FTPConnectionClosedException;
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 connects to an FTP server and retrieves the specified
032: * file. If the -s flag is used, it stores the local file at the FTP server.
033: * Just so you can see what's happening, all reply strings are printed.
034: * If the -b flag is used, a binary transfer is assumed (default is ASCII).
035: * <p>
036: * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
037: * <p>
038: ***/
039: public final class ftp {
040:
041: public static final String USAGE = "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n"
042: + "\nDefault behavior is to download a file and use ASCII transfer mode.\n"
043: + "\t-s store file on server (upload)\n"
044: + "\t-b use binary transfer mode\n";
045:
046: public static final void main(String[] args) {
047: int base = 0;
048: boolean storeFile = false, binaryTransfer = false, error = false;
049: String server, username, password, remote, local;
050: FTPClient ftp;
051:
052: for (base = 0; base < args.length; base++) {
053: if (args[base].startsWith("-s"))
054: storeFile = true;
055: else if (args[base].startsWith("-b"))
056: binaryTransfer = true;
057: else
058: break;
059: }
060:
061: if ((args.length - base) != 5) {
062: System.err.println(USAGE);
063: System.exit(1);
064: }
065:
066: server = args[base++];
067: username = args[base++];
068: password = args[base++];
069: remote = args[base++];
070: local = args[base];
071:
072: ftp = new FTPClient();
073: ftp.addProtocolCommandListener(new PrintCommandListener(
074: new PrintWriter(System.out)));
075:
076: try {
077: int reply;
078: ftp.connect(server);
079: System.out.println("Connected to " + server + ".");
080:
081: // After connection attempt, you should check the reply code to verify
082: // success.
083: reply = ftp.getReplyCode();
084:
085: if (!FTPReply.isPositiveCompletion(reply)) {
086: ftp.disconnect();
087: System.err.println("FTP server refused connection.");
088: System.exit(1);
089: }
090: } catch (IOException e) {
091: if (ftp.isConnected()) {
092: try {
093: ftp.disconnect();
094: } catch (IOException f) {
095: // do nothing
096: }
097: }
098: System.err.println("Could not connect to server.");
099: e.printStackTrace();
100: System.exit(1);
101: }
102:
103: __main: try {
104: if (!ftp.login(username, password)) {
105: ftp.logout();
106: error = true;
107: break __main;
108: }
109:
110: System.out.println("Remote system is "
111: + ftp.getSystemName());
112:
113: if (binaryTransfer)
114: ftp.setFileType(FTP.BINARY_FILE_TYPE);
115:
116: // Use passive mode as default because most of us are
117: // behind firewalls these days.
118: ftp.enterLocalPassiveMode();
119:
120: if (storeFile) {
121: InputStream input;
122:
123: input = new FileInputStream(local);
124:
125: ftp.storeFile(remote, input);
126:
127: input.close();
128: } else {
129: OutputStream output;
130:
131: output = new FileOutputStream(local);
132:
133: ftp.retrieveFile(remote, output);
134:
135: output.close();
136: }
137:
138: ftp.logout();
139: } catch (FTPConnectionClosedException e) {
140: error = true;
141: System.err.println("Server closed connection.");
142: e.printStackTrace();
143: } catch (IOException e) {
144: error = true;
145: e.printStackTrace();
146: } finally {
147: if (ftp.isConnected()) {
148: try {
149: ftp.disconnect();
150: } catch (IOException f) {
151: // do nothing
152: }
153: }
154: }
155:
156: System.exit(error ? 1 : 0);
157: } // end main
158:
159: }
|