commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rwins...@apache.org
Subject svn commit: r761990 - in /commons/proper/net/branches/NET_2_0/src/main/java/examples: FTPExample.java FTPSExample.java ftp/ ftp/FTPExample.java ftp/FTPSExample.java ftp/ServerToServerFTP.java ftp/TFTPExample.java server2serverFTP.java tftp.java
Date Sat, 04 Apr 2009 19:08:58 GMT
Author: rwinston
Date: Sat Apr  4 19:08:57 2009
New Revision: 761990

URL: http://svn.apache.org/viewvc?rev=761990&view=rev
Log:
Rename and move

Added:
    commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/
    commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java   (with
props)
    commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java   (with
props)
    commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java
  (with props)
    commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java   (with
props)
Removed:
    commons/proper/net/branches/NET_2_0/src/main/java/examples/FTPExample.java
    commons/proper/net/branches/NET_2_0/src/main/java/examples/FTPSExample.java
    commons/proper/net/branches/NET_2_0/src/main/java/examples/server2serverFTP.java
    commons/proper/net/branches/NET_2_0/src/main/java/examples/tftp.java

Added: 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=761990&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java Sat Apr
 4 19:08:57 2009
@@ -0,0 +1,192 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.ftp;
+
+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.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.FTPReply;
+
+/***
+ * This is an example program demonstrating how to use the FTPClient 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 FTPExample
+{
+
+    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;
+        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
+
+}
+

Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java?rev=761990&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java Sat Apr
 4 19:08:57 2009
@@ -0,0 +1,196 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.ftp;
+
+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 java.security.NoSuchAlgorithmException;
+
+import org.apache.commons.net.PrintCommandListener;
+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) throws NoSuchAlgorithmException
+    {
+        int base = 0;
+        boolean storeFile = false, binaryTransfer = false, error = false;
+        String server, username, password, remote, local;
+        String protocol = "SSL";    // SSL/TLS
+        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(protocol);
+       
+        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
+
+}

Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/FTPSExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java?rev=761990&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java
(added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java
Sat Apr  4 19:08:57 2009
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.ftp;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+
+import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.ProtocolCommandListener;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPReply;
+
+/***
+ * This is an example program demonstrating how to use the FTPClient class.
+ * This program arranges a server to server file transfer that transfers
+ * a file from host1 to host2.  Keep in mind, this program might only work
+ * if host2 is the same as the host you run it on (for security reasons,
+ * some ftp servers only allow PORT commands to be issued with a host
+ * argument equal to the client host).
+ * <p>
+ * Usage: ftp <host1> <user1> <pass1> <file1> <host2> <user2>
<pass2> <file2>
+ * <p>
+ ***/
+public final class ServerToServerFTP
+{
+
+    public static final void main(String[] args)
+    {
+        String server1, username1, password1, file1;
+        String server2, username2, password2, file2;
+        FTPClient ftp1, ftp2;
+        ProtocolCommandListener listener;
+
+        if (args.length < 8)
+        {
+            System.err.println(
+                "Usage: ftp <host1> <user1> <pass1> <file1> <host2>
<user2> <pass2> <file2>"
+            );
+            System.exit(1);
+        }
+
+        server1 = args[0];
+        username1 = args[1];
+        password1 = args[2];
+        file1 = args[3];
+        server2 = args[4];
+        username2 = args[5];
+        password2 = args[6];
+        file2 = args[7];
+
+        listener = new PrintCommandListener(new PrintWriter(System.out));
+        ftp1 = new FTPClient();
+        ftp1.addProtocolCommandListener(listener);
+        ftp2 = new FTPClient();
+        ftp2.addProtocolCommandListener(listener);
+
+        try
+        {
+            int reply;
+            ftp1.connect(server1);
+            System.out.println("Connected to " + server1 + ".");
+
+            reply = ftp1.getReplyCode();
+
+            if (!FTPReply.isPositiveCompletion(reply))
+            {
+                ftp1.disconnect();
+                System.err.println("FTP server1 refused connection.");
+                System.exit(1);
+            }
+        }
+        catch (IOException e)
+        {
+            if (ftp1.isConnected())
+            {
+                try
+                {
+                    ftp1.disconnect();
+                }
+                catch (IOException f)
+                {
+                    // do nothing
+                }
+            }
+            System.err.println("Could not connect to server1.");
+            e.printStackTrace();
+            System.exit(1);
+        }
+
+        try
+        {
+            int reply;
+            ftp2.connect(server2);
+            System.out.println("Connected to " + server2 + ".");
+
+            reply = ftp2.getReplyCode();
+
+            if (!FTPReply.isPositiveCompletion(reply))
+            {
+                ftp2.disconnect();
+                System.err.println("FTP server2 refused connection.");
+                System.exit(1);
+            }
+        }
+        catch (IOException e)
+        {
+            if (ftp2.isConnected())
+            {
+                try
+                {
+                    ftp2.disconnect();
+                }
+                catch (IOException f)
+                {
+                    // do nothing
+                }
+            }
+            System.err.println("Could not connect to server2.");
+            e.printStackTrace();
+            System.exit(1);
+        }
+
+__main:
+        try
+        {
+            if (!ftp1.login(username1, password1))
+            {
+                System.err.println("Could not login to " + server1);
+                break __main;
+            }
+
+            if (!ftp2.login(username2, password2))
+            {
+                System.err.println("Could not login to " + server2);
+                break __main;
+            }
+
+            // Let's just assume success for now.
+            ftp2.enterRemotePassiveMode();
+
+            ftp1.enterRemoteActiveMode(InetAddress.getByName(ftp2.getPassiveHost()),
+                                       ftp2.getPassivePort());
+
+            // Although you would think the store command should be sent to server2
+            // first, in reality, ftp servers like wu-ftpd start accepting data
+            // connections right after entering passive mode.  Additionally, they
+            // don't even send the positive preliminary reply until after the
+            // transfer is completed (in the case of passive mode transfers).
+            // Therefore, calling store first would hang waiting for a preliminary
+            // reply.
+            if (ftp1.remoteRetrieve(file1) && ftp2.remoteStoreUnique(file2))
+            {
+                //      if(ftp1.remoteRetrieve(file1) && ftp2.remoteStore(file2))
{
+                // We have to fetch the positive completion reply.
+                ftp1.completePendingCommand();
+                ftp2.completePendingCommand();
+            }
+            else
+            {
+                System.err.println(
+                    "Couldn't initiate transfer.  Check that filenames are valid.");
+                break __main;
+            }
+
+        }
+        catch (IOException e)
+        {
+            e.printStackTrace();
+            System.exit(1);
+        }
+        finally
+        {
+            try
+            {
+                if (ftp1.isConnected())
+                {
+                    ftp1.logout();
+                    ftp1.disconnect();
+                }
+            }
+            catch (IOException e)
+            {
+                // do nothing
+            }
+
+            try
+            {
+                if (ftp2.isConnected())
+                {
+                    ftp2.logout();
+                    ftp2.disconnect();
+                }
+            }
+            catch (IOException e)
+            {
+                // do nothing
+            }
+        }
+    }
+}

Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/ServerToServerFTP.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java
URL: http://svn.apache.org/viewvc/commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java?rev=761990&view=auto
==============================================================================
--- commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java (added)
+++ commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java Sat Apr
 4 19:08:57 2009
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.ftp;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import org.apache.commons.net.tftp.TFTP;
+import org.apache.commons.net.tftp.TFTPClient;
+
+/***
+ * This is an example of a simple Java tftp client using NetComponents.
+ * Notice how all of the code is really just argument processing and
+ * error handling.
+ * <p>
+ * Usage: tftp [options] hostname localfile remotefile
+ * hostname   - The name of the remote host
+ * localfile  - The name of the local file to send or the name to use for
+ *              the received file
+ * remotefile - The name of the remote file to receive or the name for
+ *              the remote server to use to name the local file being sent.
+ * options: (The default is to assume -r -b)
+ *        -s Send a local file
+ *        -r Receive a remote file
+ *        -a Use ASCII transfer mode
+ *        -b Use binary transfer mode
+ * <p>
+ ***/
+public final class TFTPExample
+{
+    static final String USAGE =
+        "Usage: tftp [options] hostname localfile remotefile\n\n" +
+        "hostname   - The name of the remote host\n" +
+        "localfile  - The name of the local file to send or the name to use for\n" +
+        "\tthe received file\n" +
+        "remotefile - The name of the remote file to receive or the name for\n" +
+        "\tthe remote server to use to name the local file being sent.\n\n" +
+        "options: (The default is to assume -r -b)\n" +
+        "\t-s Send a local file\n" +
+        "\t-r Receive a remote file\n" +
+        "\t-a Use ASCII transfer mode\n" +
+        "\t-b Use binary transfer mode\n";
+
+    public final static void main(String[] args)
+    {
+        boolean receiveFile = true, closed;
+        int transferMode = TFTP.BINARY_MODE, argc;
+        String arg, hostname, localFilename, remoteFilename;
+        TFTPClient tftp;
+
+        // Parse options
+        for (argc = 0; argc < args.length; argc++)
+        {
+            arg = args[argc];
+            if (arg.startsWith("-"))
+            {
+                if (arg.equals("-r"))
+                    receiveFile = true;
+                else if (arg.equals("-s"))
+                    receiveFile = false;
+                else if (arg.equals("-a"))
+                    transferMode = TFTP.ASCII_MODE;
+                else if (arg.equals("-b"))
+                    transferMode = TFTP.BINARY_MODE;
+                else
+                {
+                    System.err.println("Error: unrecognized option.");
+                    System.err.print(USAGE);
+                    System.exit(1);
+                }
+            }
+            else
+                break;
+        }
+
+        // Make sure there are enough arguments
+        if (args.length - argc != 3)
+        {
+            System.err.println("Error: invalid number of arguments.");
+            System.err.print(USAGE);
+            System.exit(1);
+        }
+
+        // Get host and file arguments
+        hostname = args[argc];
+        localFilename = args[argc + 1];
+        remoteFilename = args[argc + 2];
+
+        // Create our TFTP instance to handle the file transfer.
+        tftp = new TFTPClient();
+
+        // We want to timeout if a response takes longer than 60 seconds
+        tftp.setDefaultTimeout(60000);
+
+        // Open local socket
+        try
+        {
+            tftp.open();
+        }
+        catch (SocketException e)
+        {
+            System.err.println("Error: could not open local UDP socket.");
+            System.err.println(e.getMessage());
+            System.exit(1);
+        }
+
+        // We haven't closed the local file yet.
+        closed = false;
+
+        // If we're receiving a file, receive, otherwise send.
+        if (receiveFile)
+        {
+            FileOutputStream output = null;
+            File file;
+
+            file = new File(localFilename);
+
+            // If file exists, don't overwrite it.
+            if (file.exists())
+            {
+                System.err.println("Error: " + localFilename + " already exists.");
+                System.exit(1);
+            }
+
+            // Try to open local file for writing
+            try
+            {
+                output = new FileOutputStream(file);
+            }
+            catch (IOException e)
+            {
+                tftp.close();
+                System.err.println("Error: could not open local file for writing.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+
+            // Try to receive remote file via TFTP
+            try
+            {
+                tftp.receiveFile(remoteFilename, transferMode, output, hostname);
+            }
+            catch (UnknownHostException e)
+            {
+                System.err.println("Error: could not resolve hostname.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+            catch (IOException e)
+            {
+                System.err.println(
+                    "Error: I/O exception occurred while receiving file.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+            finally
+            {
+                // Close local socket and output file
+                tftp.close();
+                try
+                {
+                    output.close();
+                    closed = true;
+                }
+                catch (IOException e)
+                {
+                    closed = false;
+                    System.err.println("Error: error closing file.");
+                    System.err.println(e.getMessage());
+                }
+            }
+
+            if (!closed)
+                System.exit(1);
+
+        }
+        else
+        {
+            // We're sending a file
+            FileInputStream input = null;
+
+            // Try to open local file for reading
+            try
+            {
+                input = new FileInputStream(localFilename);
+            }
+            catch (IOException e)
+            {
+                tftp.close();
+                System.err.println("Error: could not open local file for reading.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+
+            // Try to send local file via TFTP
+            try
+            {
+                tftp.sendFile(remoteFilename, transferMode, input, hostname);
+            }
+            catch (UnknownHostException e)
+            {
+                System.err.println("Error: could not resolve hostname.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+            catch (IOException e)
+            {
+                System.err.println(
+                    "Error: I/O exception occurred while sending file.");
+                System.err.println(e.getMessage());
+                System.exit(1);
+            }
+            finally
+            {
+                // Close local socket and input file
+                tftp.close();
+                try
+                {
+                    input.close();
+                    closed = true;
+                }
+                catch (IOException e)
+                {
+                    closed = false;
+                    System.err.println("Error: error closing file.");
+                    System.err.println(e.getMessage());
+                }
+            }
+
+            if (!closed)
+                System.exit(1);
+
+        }
+
+    }
+
+}
+
+

Propchange: commons/proper/net/branches/NET_2_0/src/main/java/examples/ftp/TFTPExample.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message