KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > ftp


1 /*
2  * Copyright 2001-2005 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package examples;
17
18 import java.io.FileInputStream JavaDoc;
19 import java.io.FileOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import org.apache.commons.net.ftp.FTP;
25 import org.apache.commons.net.ftp.FTPClient;
26 import org.apache.commons.net.ftp.FTPConnectionClosedException;
27 import org.apache.commons.net.ftp.FTPReply;
28
29 /***
30  * This is an example program demonstrating how to use the FTPClient class.
31  * This program connects to an FTP server and retrieves the specified
32  * file. If the -s flag is used, it stores the local file at the FTP server.
33  * Just so you can see what's happening, all reply strings are printed.
34  * If the -b flag is used, a binary transfer is assumed (default is ASCII).
35  * <p>
36  * Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>
37  * <p>
38  ***/

39 public final class ftp
40 {
41
42     public static final String JavaDoc USAGE =
43         "Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
44         "\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
45         "\t-s store file on server (upload)\n" +
46         "\t-b use binary transfer mode\n";
47
48     public static final void main(String JavaDoc[] args)
49     {
50         int base = 0;
51         boolean storeFile = false, binaryTransfer = false, error = false;
52         String JavaDoc server, username, password, remote, local;
53         FTPClient ftp;
54
55         for (base = 0; base < args.length; base++)
56         {
57             if (args[base].startsWith("-s"))
58                 storeFile = true;
59             else if (args[base].startsWith("-b"))
60                 binaryTransfer = true;
61             else
62                 break;
63         }
64
65         if ((args.length - base) != 5)
66         {
67             System.err.println(USAGE);
68             System.exit(1);
69         }
70
71         server = args[base++];
72         username = args[base++];
73         password = args[base++];
74         remote = args[base++];
75         local = args[base];
76
77         ftp = new FTPClient();
78         ftp.addProtocolCommandListener(new PrintCommandListener(
79                                            new PrintWriter JavaDoc(System.out)));
80
81         try
82         {
83             int reply;
84             ftp.connect(server);
85             System.out.println("Connected to " + server + ".");
86
87             // After connection attempt, you should check the reply code to verify
88
// success.
89
reply = ftp.getReplyCode();
90
91             if (!FTPReply.isPositiveCompletion(reply))
92             {
93                 ftp.disconnect();
94                 System.err.println("FTP server refused connection.");
95                 System.exit(1);
96             }
97         }
98         catch (IOException JavaDoc e)
99         {
100             if (ftp.isConnected())
101             {
102                 try
103                 {
104                     ftp.disconnect();
105                 }
106                 catch (IOException JavaDoc f)
107                 {
108                     // do nothing
109
}
110             }
111             System.err.println("Could not connect to server.");
112             e.printStackTrace();
113             System.exit(1);
114         }
115
116 __main:
117         try
118         {
119             if (!ftp.login(username, password))
120             {
121                 ftp.logout();
122                 error = true;
123                 break __main;
124             }
125
126             System.out.println("Remote system is " + ftp.getSystemName());
127
128             if (binaryTransfer)
129                 ftp.setFileType(FTP.BINARY_FILE_TYPE);
130
131             // Use passive mode as default because most of us are
132
// behind firewalls these days.
133
ftp.enterLocalPassiveMode();
134
135             if (storeFile)
136             {
137                 InputStream JavaDoc input;
138
139                 input = new FileInputStream JavaDoc(local);
140
141                 ftp.storeFile(remote, input);
142
143                 input.close();
144             }
145             else
146             {
147                 OutputStream JavaDoc output;
148
149                 output = new FileOutputStream JavaDoc(local);
150
151                 ftp.retrieveFile(remote, output);
152
153                 output.close();
154             }
155
156             ftp.logout();
157         }
158         catch (FTPConnectionClosedException e)
159         {
160             error = true;
161             System.err.println("Server closed connection.");
162             e.printStackTrace();
163         }
164         catch (IOException JavaDoc e)
165         {
166             error = true;
167             e.printStackTrace();
168         }
169         finally
170         {
171             if (ftp.isConnected())
172             {
173                 try
174                 {
175                     ftp.disconnect();
176                 }
177                 catch (IOException JavaDoc f)
178                 {
179                     // do nothing
180
}
181             }
182         }
183
184         System.exit(error ? 1 : 0);
185     } // end main
186

187 }
188
189
Popular Tags