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: static final String USAGE = "Usage: tftp [options] hostname localfile remotefile\n\n"
047: + "hostname - The name of the remote host\n"
048: + "localfile - The name of the local file to send or the name to use for\n"
049: + "\tthe received file\n"
050: + "remotefile - The name of the remote file to receive or the name for\n"
051: + "\tthe remote server to use to name the local file being sent.\n\n"
052: + "options: (The default is to assume -r -b)\n"
053: + "\t-s Send a local file\n"
054: + "\t-r Receive a remote file\n"
055: + "\t-a Use ASCII transfer mode\n"
056: + "\t-b Use binary transfer mode\n";
057:
058: public final static void main(String[] args) {
059: boolean receiveFile = true, closed;
060: int transferMode = TFTP.BINARY_MODE, argc;
061: String arg, hostname, localFilename, remoteFilename;
062: TFTPClient tftp;
063:
064: // Parse options
065: for (argc = 0; argc < args.length; argc++) {
066: arg = args[argc];
067: if (arg.startsWith("-")) {
068: if (arg.equals("-r"))
069: receiveFile = true;
070: else if (arg.equals("-s"))
071: receiveFile = false;
072: else if (arg.equals("-a"))
073: transferMode = TFTP.ASCII_MODE;
074: else if (arg.equals("-b"))
075: transferMode = TFTP.BINARY_MODE;
076: else {
077: System.err.println("Error: unrecognized option.");
078: System.err.print(USAGE);
079: System.exit(1);
080: }
081: } else
082: break;
083: }
084:
085: // Make sure there are enough arguments
086: if (args.length - argc != 3) {
087: System.err.println("Error: invalid number of arguments.");
088: System.err.print(USAGE);
089: System.exit(1);
090: }
091:
092: // Get host and file arguments
093: hostname = args[argc];
094: localFilename = args[argc + 1];
095: remoteFilename = args[argc + 2];
096:
097: // Create our TFTP instance to handle the file transfer.
098: tftp = new TFTPClient();
099:
100: // We want to timeout if a response takes longer than 60 seconds
101: tftp.setDefaultTimeout(60000);
102:
103: // Open local socket
104: try {
105: tftp.open();
106: } catch (SocketException e) {
107: System.err
108: .println("Error: could not open local UDP socket.");
109: System.err.println(e.getMessage());
110: System.exit(1);
111: }
112:
113: // We haven't closed the local file yet.
114: closed = false;
115:
116: // If we're receiving a file, receive, otherwise send.
117: if (receiveFile) {
118: FileOutputStream output = null;
119: File file;
120:
121: file = new File(localFilename);
122:
123: // If file exists, don't overwrite it.
124: if (file.exists()) {
125: System.err.println("Error: " + localFilename
126: + " already exists.");
127: System.exit(1);
128: }
129:
130: // Try to open local file for writing
131: try {
132: output = new FileOutputStream(file);
133: } catch (IOException e) {
134: tftp.close();
135: System.err
136: .println("Error: could not open local file for writing.");
137: System.err.println(e.getMessage());
138: System.exit(1);
139: }
140:
141: // Try to receive remote file via TFTP
142: try {
143: tftp.receiveFile(remoteFilename, transferMode, output,
144: hostname);
145: } catch (UnknownHostException e) {
146: System.err
147: .println("Error: could not resolve hostname.");
148: System.err.println(e.getMessage());
149: System.exit(1);
150: } catch (IOException e) {
151: System.err
152: .println("Error: I/O exception occurred while receiving file.");
153: System.err.println(e.getMessage());
154: System.exit(1);
155: } finally {
156: // Close local socket and output file
157: tftp.close();
158: try {
159: output.close();
160: closed = true;
161: } catch (IOException e) {
162: closed = false;
163: System.err.println("Error: error closing file.");
164: System.err.println(e.getMessage());
165: }
166: }
167:
168: if (!closed)
169: System.exit(1);
170:
171: } else {
172: // We're sending a file
173: FileInputStream input = null;
174:
175: // Try to open local file for reading
176: try {
177: input = new FileInputStream(localFilename);
178: } catch (IOException e) {
179: tftp.close();
180: System.err
181: .println("Error: could not open local file for reading.");
182: System.err.println(e.getMessage());
183: System.exit(1);
184: }
185:
186: // Try to send local file via TFTP
187: try {
188: tftp.sendFile(remoteFilename, transferMode, input,
189: hostname);
190: } catch (UnknownHostException e) {
191: System.err
192: .println("Error: could not resolve hostname.");
193: System.err.println(e.getMessage());
194: System.exit(1);
195: } catch (IOException e) {
196: System.err
197: .println("Error: I/O exception occurred while sending file.");
198: System.err.println(e.getMessage());
199: System.exit(1);
200: } finally {
201: // Close local socket and input file
202: tftp.close();
203: try {
204: input.close();
205: closed = true;
206: } catch (IOException e) {
207: closed = false;
208: System.err.println("Error: error closing file.");
209: System.err.println(e.getMessage());
210: }
211: }
212:
213: if (!closed)
214: System.exit(1);
215:
216: }
217:
218: }
219:
220: }
|