commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwins...@apache.org
Subject svn commit: r437132 - in /jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java: examples/FTPSExample.java org/apache/commons/net/ftp/FTPSClient.java
Date Sat, 26 Aug 2006 09:24:14 GMT
Author: rwinston
Date: Sat Aug 26 02:24:11 2006
New Revision: 437132

URL: http://svn.apache.org/viewvc?rev=437132&view=rev
Log:
Added FTPS example from José's submission and added default ctor to FTPSClient.java. Also
cleaned up some Javadoc.

Added:
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/examples/FTPSExample.java
Modified:
    jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/ftp/FTPSClient.java

Added: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/examples/FTPSExample.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/examples/FTPSExample.java?rev=437132&view=auto
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/examples/FTPSExample.java
(added)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/examples/FTPSExample.java
Sat Aug 26 02:24:11 2006
@@ -0,0 +1,191 @@
+/*
+ * Copyright 2001-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package examples;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPConnectionClosedException;
+import org.apache.commons.net.ftp.FTPReply;
+import org.apache.commons.net.ftp.FTPSClient;
+
+/***
+ * This is an example program demonstrating how to use the FTPSClient class.
+ * This program connects to an FTP server and retrieves the specified
+ * file.  If the -s flag is used, it stores the local file at the FTP server.
+ * Just so you can see what's happening, all reply strings are printed.
+ * If the -b flag is used, a binary transfer is assumed (default is ASCII).
+ * <p>
+ * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file>
<local file>
+ * <p>
+ ***/
+public final class FTPSExample
+{
+
+    public static final String USAGE =
+        "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote
file> <local file>\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)
+    {
+    	int base = 0;
+        boolean storeFile = false, binaryTransfer = false, error = false;
+        String server, username, password, remote, local;
+        FTPSClient ftps;
+        
+        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];
+
+        ftps = new FTPSClient("JKS","SSL","password","0","P");
+        // NOTE this is necessary for FTPSClient connections
+		ftps.setReaderThread(false);
+        ftps.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
+
+        try
+        {
+            int reply;
+
+            ftps.connect(server);
+            System.out.println("Connected to " + server + ".");
+
+            // After connection attempt, you should check the reply code to verify
+            // success.
+            reply = ftps.getReplyCode();
+
+            if (!FTPReply.isPositiveCompletion(reply))
+            {
+                ftps.disconnect();
+                System.err.println("FTP server refused connection.");
+                System.exit(1);
+            }
+        }
+        catch (IOException e)
+        {
+            if (ftps.isConnected())
+            {
+                try
+                {
+                    ftps.disconnect();
+                }
+                catch (IOException f)
+                {
+                    // do nothing
+                }
+            }
+            System.err.println("Could not connect to server.");
+            e.printStackTrace();
+            System.exit(1);
+        }
+
+__main:
+        try
+        {
+            ftps.setBufferSize(1000);
+
+        	if (!ftps.login(username, password))
+            {
+                ftps.logout();
+                error = true;
+                break __main;
+            }
+
+            
+            System.out.println("Remote system is " + ftps.getSystemName());
+
+            if (binaryTransfer) ftps.setFileType(FTP.BINARY_FILE_TYPE);
+
+            // Use passive mode as default because most of us are
+            // behind firewalls these days.
+            ftps.enterLocalPassiveMode();
+
+            if (storeFile)
+            {
+                InputStream input;
+
+                input = new FileInputStream(local);
+
+                ftps.storeFile(remote, input);
+
+                input.close();
+            }
+            else
+            {
+                OutputStream output;
+
+                output = new FileOutputStream(local);
+
+                ftps.retrieveFile(remote, output);
+
+                output.close();
+            }
+
+            ftps.logout();
+        }
+        catch (FTPConnectionClosedException e)
+        {
+            error = true;
+            System.err.println("Server closed connection.");
+            e.printStackTrace();
+        }
+        catch (IOException e)
+        {
+            error = true;
+            e.printStackTrace();
+        }
+        finally
+        {
+            if (ftps.isConnected())
+            {
+                try
+                {
+                    ftps.disconnect();
+                }
+                catch (IOException f)
+                {
+                    // do nothing
+                }
+            }
+        }
+
+        System.exit(error ? 1 : 0);
+    } // end main
+
+}

Modified: jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/ftp/FTPSClient.java?rev=437132&r1=437131&r2=437132&view=diff
==============================================================================
--- jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
(original)
+++ jakarta/commons/proper/net/branches/JDK_1_5_BRANCH/src/main/java/org/apache/commons/net/ftp/FTPSClient.java
Sat Aug 26 02:24:11 2006
@@ -64,6 +64,8 @@
 	 * @param keyStoreName Type of instance KeyStore, JKS for Java 1.3 y JCEKS for Java 1.4

 	 * @param sslContext Type of the instance SSLContext, can be SSL or TLS.
 	 * @param password The password to access the KeyStore.
+	 * @param pbsz Protection buffer size (Use 0 to indicate streaming) 
+	 * @param prot The protection level for the data channel
 	 */
 	public FTPSClient(String keyStoreName, String sslContext, String password, String pbsz,
String prot) {
 		this.sslContext = sslContext;



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message