Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 36327 invoked from network); 20 Mar 2010 04:17:28 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 20 Mar 2010 04:17:28 -0000 Received: (qmail 31924 invoked by uid 500); 20 Mar 2010 04:17:28 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 31590 invoked by uid 500); 20 Mar 2010 04:17:25 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 31583 invoked by uid 99); 20 Mar 2010 04:17:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Mar 2010 04:17:24 +0000 X-ASF-Spam-Status: No, hits=-1896.7 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 20 Mar 2010 04:17:23 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2AD5C23888FE; Sat, 20 Mar 2010 04:17:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r925541 - /commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java Date: Sat, 20 Mar 2010 04:17:02 -0000 To: commits@commons.apache.org From: rwinston@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100320041703.2AD5C23888FE@eris.apache.org> Author: rwinston Date: Sat Mar 20 04:17:00 2010 New Revision: 925541 URL: http://svn.apache.org/viewvc?rev=925541&view=rev Log: Revert Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java Modified: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java?rev=925541&r1=925540&r2=925541&view=diff ============================================================================== --- commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java (original) +++ commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java Sat Mar 20 04:17:00 2010 @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + package examples.ftp; import java.io.FileInputStream; @@ -22,14 +22,12 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.PrintStream; import java.io.PrintWriter; import org.apache.commons.net.PrintCommandListener; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPConnectionClosedException; -import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /*** @@ -42,22 +40,152 @@ import org.apache.commons.net.ftp.FTPRep * Usage: ftp [-s] [-b] *

***/ -public final class FTPExample { +public final class FTPExample +{ + + public static final String USAGE = + "Usage: ftp [-s] [-b] \n" + + "\nDefault behavior is to download a file and use ASCII transfer mode.\n" + + "\t-s store file on server (upload)\n" + + "\t-b use binary transfer mode\n"; - public static final void main(String[] args) throws IOException + public static final void main(String[] args) { - FTPClient client = new FTPClient(); - client.setControlEncoding("utf-8"); - client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); - client.connect("localhost"); - client.login("rory", "yi6bovak"); - - PrintStream out = new PrintStream(System.out, true, "utf-8"); - - for (FTPFile file : client.listFiles()) - out.println(file.getName()); - - out.println("你好吗"); + int base = 0; + boolean storeFile = false, binaryTransfer = false, error = false; + String server, username, password, remote, local; + FTPClient ftp; + + for (base = 0; base < args.length; base++) + { + if (args[base].startsWith("-s")) + storeFile = true; + else if (args[base].startsWith("-b")) + binaryTransfer = true; + else + break; + } + + if ((args.length - base) != 5) + { + System.err.println(USAGE); + System.exit(1); + } + + server = args[base++]; + username = args[base++]; + password = args[base++]; + remote = args[base++]; + local = args[base]; + + ftp = new FTPClient(); + ftp.addProtocolCommandListener(new PrintCommandListener( + new PrintWriter(System.out))); + + try + { + int reply; + ftp.connect(server); + System.out.println("Connected to " + server + "."); + + // After connection attempt, you should check the reply code to verify + // success. + reply = ftp.getReplyCode(); + + if (!FTPReply.isPositiveCompletion(reply)) + { + ftp.disconnect(); + System.err.println("FTP server refused connection."); + System.exit(1); + } + } + catch (IOException e) + { + if (ftp.isConnected()) + { + try + { + ftp.disconnect(); + } + catch (IOException f) + { + // do nothing + } + } + System.err.println("Could not connect to server."); + e.printStackTrace(); + System.exit(1); + } + +__main: + try + { + if (!ftp.login(username, password)) + { + ftp.logout(); + error = true; + break __main; + } + + System.out.println("Remote system is " + ftp.getSystemName()); + + if (binaryTransfer) + ftp.setFileType(FTP.BINARY_FILE_TYPE); + + // Use passive mode as default because most of us are + // behind firewalls these days. + ftp.enterLocalPassiveMode(); + + if (storeFile) + { + InputStream input; + + input = new FileInputStream(local); + + ftp.storeFile(remote, input); + + input.close(); + } + else + { + OutputStream output; + + output = new FileOutputStream(local); + + ftp.retrieveFile(remote, output); + + output.close(); + } + + ftp.logout(); + } + catch (FTPConnectionClosedException e) + { + error = true; + System.err.println("Server closed connection."); + e.printStackTrace(); + } + catch (IOException e) + { + error = true; + e.printStackTrace(); + } + finally + { + if (ftp.isConnected()) + { + try + { + ftp.disconnect(); + } + catch (IOException f) + { + // do nothing + } + } + } + + System.exit(error ? 1 : 0); } // end main }