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.File;
019    import java.io.FileInputStream;
020    import java.io.FileOutputStream;
021    import java.io.IOException;
022    import java.net.SocketException;
023    import java.net.UnknownHostException;
024    import org.apache.commons.net.tftp.TFTP;
025    import org.apache.commons.net.tftp.TFTPClient;
026    
027    /***
028     * This is an example of a simple Java tftp client using NetComponents.
029     * Notice how all of the code is really just argument processing and
030     * error handling.
031     * <p>
032     * Usage: tftp [options] hostname localfile remotefile
033     * hostname   - The name of the remote host
034     * localfile  - The name of the local file to send or the name to use for
035     *              the received file
036     * remotefile - The name of the remote file to receive or the name for
037     *              the remote server to use to name the local file being sent.
038     * options: (The default is to assume -r -b)
039     *        -s Send a local file
040     *        -r Receive a remote file
041     *        -a Use ASCII transfer mode
042     *        -b Use binary transfer mode
043     * <p>
044     ***/
045    public final class tftp
046    {
047        static final String USAGE =
048            "Usage: tftp [options] hostname localfile remotefile\n\n" +
049            "hostname   - The name of the remote host\n" +
050            "localfile  - The name of the local file to send or the name to use for\n" +
051            "\tthe received file\n" +
052            "remotefile - The name of the remote file to receive or the name for\n" +
053            "\tthe remote server to use to name the local file being sent.\n\n" +
054            "options: (The default is to assume -r -b)\n" +
055            "\t-s Send a local file\n" +
056            "\t-r Receive a remote file\n" +
057            "\t-a Use ASCII transfer mode\n" +
058            "\t-b Use binary transfer mode\n";
059    
060        public final static void main(String[] args)
061        {
062            boolean receiveFile = true, closed;
063            int transferMode = TFTP.BINARY_MODE, argc;
064            String arg, hostname, localFilename, remoteFilename;
065            TFTPClient tftp;
066    
067            // Parse options
068            for (argc = 0; argc < args.length; argc++)
069            {
070                arg = args[argc];
071                if (arg.startsWith("-"))
072                {
073                    if (arg.equals("-r"))
074                        receiveFile = true;
075                    else if (arg.equals("-s"))
076                        receiveFile = false;
077                    else if (arg.equals("-a"))
078                        transferMode = TFTP.ASCII_MODE;
079                    else if (arg.equals("-b"))
080                        transferMode = TFTP.BINARY_MODE;
081                    else
082                    {
083                        System.err.println("Error: unrecognized option.");
084                        System.err.print(USAGE);
085                        System.exit(1);
086                    }
087                }
088                else
089                    break;
090            }
091    
092            // Make sure there are enough arguments
093            if (args.length - argc != 3)
094            {
095                System.err.println("Error: invalid number of arguments.");
096                System.err.print(USAGE);
097                System.exit(1);
098            }
099    
100            // Get host and file arguments
101            hostname = args[argc];
102            localFilename = args[argc + 1];
103            remoteFilename = args[argc + 2];
104    
105            // Create our TFTP instance to handle the file transfer.
106            tftp = new TFTPClient();
107    
108            // We want to timeout if a response takes longer than 60 seconds
109            tftp.setDefaultTimeout(60000);
110    
111            // Open local socket
112            try
113            {
114                tftp.open();
115            }
116            catch (SocketException e)
117            {
118                System.err.println("Error: could not open local UDP socket.");
119                System.err.println(e.getMessage());
120                System.exit(1);
121            }
122    
123            // We haven't closed the local file yet.
124            closed = false;
125    
126            // If we're receiving a file, receive, otherwise send.
127            if (receiveFile)
128            {
129                FileOutputStream output = null;
130                File file;
131    
132                file = new File(localFilename);
133    
134                // If file exists, don't overwrite it.
135                if (file.exists())
136                {
137                    System.err.println("Error: " + localFilename + " already exists.");
138                    System.exit(1);
139                }
140    
141                // Try to open local file for writing
142                try
143                {
144                    output = new FileOutputStream(file);
145                }
146                catch (IOException e)
147                {
148                    tftp.close();
149                    System.err.println("Error: could not open local file for writing.");
150                    System.err.println(e.getMessage());
151                    System.exit(1);
152                }
153    
154                // Try to receive remote file via TFTP
155                try
156                {
157                    tftp.receiveFile(remoteFilename, transferMode, output, hostname);
158                }
159                catch (UnknownHostException e)
160                {
161                    System.err.println("Error: could not resolve hostname.");
162                    System.err.println(e.getMessage());
163                    System.exit(1);
164                }
165                catch (IOException e)
166                {
167                    System.err.println(
168                        "Error: I/O exception occurred while receiving file.");
169                    System.err.println(e.getMessage());
170                    System.exit(1);
171                }
172                finally
173                {
174                    // Close local socket and output file
175                    tftp.close();
176                    try
177                    {
178                        output.close();
179                        closed = true;
180                    }
181                    catch (IOException e)
182                    {
183                        closed = false;
184                        System.err.println("Error: error closing file.");
185                        System.err.println(e.getMessage());
186                    }
187                }
188    
189                if (!closed)
190                    System.exit(1);
191    
192            }
193            else
194            {
195                // We're sending a file
196                FileInputStream input = null;
197    
198                // Try to open local file for reading
199                try
200                {
201                    input = new FileInputStream(localFilename);
202                }
203                catch (IOException e)
204                {
205                    tftp.close();
206                    System.err.println("Error: could not open local file for reading.");
207                    System.err.println(e.getMessage());
208                    System.exit(1);
209                }
210    
211                // Try to send local file via TFTP
212                try
213                {
214                    tftp.sendFile(remoteFilename, transferMode, input, hostname);
215                }
216                catch (UnknownHostException e)
217                {
218                    System.err.println("Error: could not resolve hostname.");
219                    System.err.println(e.getMessage());
220                    System.exit(1);
221                }
222                catch (IOException e)
223                {
224                    System.err.println(
225                        "Error: I/O exception occurred while sending file.");
226                    System.err.println(e.getMessage());
227                    System.exit(1);
228                }
229                finally
230                {
231                    // Close local socket and input file
232                    tftp.close();
233                    try
234                    {
235                        input.close();
236                        closed = true;
237                    }
238                    catch (IOException e)
239                    {
240                        closed = false;
241                        System.err.println("Error: error closing file.");
242                        System.err.println(e.getMessage());
243                    }
244                }
245    
246                if (!closed)
247                    System.exit(1);
248    
249            }
250    
251        }
252    
253    }
254    
255