1 package org.apache.commons.net.ftp;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedInputStream;
58 import java.io.BufferedOutputStream;
59 import java.io.BufferedReader;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.io.InputStreamReader;
63 import java.io.OutputStream;
64 import java.net.InetAddress;
65 import java.net.ServerSocket;
66 import java.net.Socket;
67 import java.util.Vector;
68 import org.apache.commons.net.io.FromNetASCIIInputStream;
69 import org.apache.commons.net.io.ToNetASCIIOutputStream;
70 import org.apache.commons.net.io.Util;
71 import org.apache.commons.net.MalformedServerReplyException;
72
73 /****
74 * FTPClient encapsulates all the functionality necessary to store and
75 * retrieve files from an FTP server. This class takes care of all
76 * low level details of interacting with an FTP server and provides
77 * a convenient higher level interface. As with all classes derived
78 * from <a href="org.apache.commons.net.SocketClient.html"> SocketClient </a>,
79 * you must first connect to the server with
80 * <a href="org.apache.commons.net.SocketClient.html#connect"> connect </a>
81 * before doing anything, and finally
82 * <a href="org.apache.commons.net.SocketClient.html#disconnect"> disconnect </a>
83 * after you're completely finished interacting with the server.
84 * Then you need to check the FTP reply code to see if the connection
85 * was successful. For example:
86 * <pre>
87 * try {
88 * int reply;
89 * ftp.connect("ftp.foobar.com");
90 * System.out.println("Connected to " + server + ".");
91 * System.out.print(ftp.getReplyString());
92 *
93 * // After connection attempt, you should check the reply code to verify
94 * // success.
95 * reply = ftp.getReplyCode();
96 *
97 * if(!FTPReply.isPositiveCompletion(reply)) {
98 * ftp.disconnect();
99 * System.err.println("FTP server refused connection.");
100 * System.exit(1);
101 * }
102 * } catch(IOException e) {
103 * if(ftp.isConnected()) {
104 * try {
105 * ftp.disconnect();
106 * } catch(IOException f) {
107 * // do nothing
108 * }
109 * }
110 * System.err.println("Could not connect to server.");
111 * e.printStackTrace();
112 * System.exit(1);
113 * }
114 * </pre>
115 * <p>
116 * Immediately after connecting is the only real time you need to check the
117 * reply code (because connect is of type void). The convention for all the
118 * FTP command methods in FTPClient is such that they either return a
119 * boolean value or some other value.
120 * The boolean methods return true on a successful completion reply from
121 * the FTP server and false on a reply resulting in an error condition or
122 * failure. The methods returning a value other than boolean return a value
123 * containing the higher level data produced by the FTP command, or null if a
124 * reply resulted in an error condition or failure. If you want to access
125 * the exact FTP reply code causing a success or failure, you must call
126 * <a href="org.apache.commons.net.ftp.FTP.html#getReplyCode"> getReplyCode </a> after
127 * a success or failure.
128 * <p>
129 * The default settings for FTPClient are for it to use
130 * <code> FTP.ASCII_FILE_TYPE </code>,
131 * <code> FTP.NON_PRINT_TEXT_FORMAT </code>,
132 * <code> FTP.STREAM_TRANSFER_MODE </code>, and
133 * <code> FTP.FILE_STRUCTURE </code>. The only file types directly supported
134 * are <code> FTP.ASCII_FILE_TYPE </code> and
135 * <code> FTP.IMAGE_FILE_TYPE </code> (which is the same as
136 * <code> FTP.BINARY_FILE_TYPE </code>). Because there are at lest 4
137 * different EBCDIC encodings, we have opted not to provide direct support
138 * for EBCDIC. To transfer EBCDIC and other unsupported file types you
139 * must create your own filter InputStreams and OutputStreams and wrap
140 * them around the streams returned or required by the FTPClient methods.
141 * FTPClient uses the NetASCII filter streams in
142 * <a href="Package-org.apache.commons.net.io.html"> org.apache.commons.net.io </a> to provide
143 * transparent handling of ASCII files. We will consider incorporating
144 * EBCDIC support if there is enough demand.
145 * <p>
146 * <code> FTP.NON_PRINT_TEXT_FORMAT </code>,
147 * <code> FTP.STREAM_TRANSFER_MODE </code>, and
148 * <code> FTP.FILE_STRUCTURE </code> are the only supported formats,
149 * transfer modes, and file structures.
150 * <p>
151 * Because the handling of sockets on different platforms can differ
152 * significantly, the FTPClient automatically issues a new PORT command
153 * prior to every transfer requiring that the server connect to the client's
154 * data port. This ensures identical problem-free behavior on Windows, Unix,
155 * and Macintosh platforms. Additionally, it relieves programmers from
156 * having to issue the PORT command themselves and dealing with platform
157 * dependent issues.
158 * <p>
159 * Additionally, for security purposes, all data connections to the
160 * client are verified to ensure that they originated from the intended
161 * party (host and port). If a data connection is initiated by an unexpected
162 * party, the command will close the socket and throw an IOException. You
163 * may disable this behavior with
164 * <a href="#setRemoteVerificationEnabled">setRemoteVerificationEnabled()</a>.
165 * <p>
166 * You should keep in mind that the FTP server may choose to prematurely
167 * close a connection if the client has been idle for longer than a
168 * given time period (usually 900 seconds). The FTPClient class will detect a
169 * premature FTP server connection closing when it receives a
170 * <a href="org.apache.commons.net.ftp.FTPReply.html#SERVICE_NOT_AVAILABLE">
171 * FTPReply.SERVICE_NOT_AVAILABLE </a> response to a command.
172 * When that occurs, the FTP class method encountering that reply will throw
173 * an <a href="org.apache.commons.net.ftp.FTPConnectionClosedException.html">
174 * FTPConnectionClosedException </a>.
175 * <code>FTPConnectionClosedException</code>
176 * is a subclass of <code> IOException </code> and therefore need not be
177 * caught separately, but if you are going to catch it separately, its
178 * catch block must appear before the more general <code> IOException </code>
179 * catch block. When you encounter an
180 * <a href="org.apache.commons.net.ftp.FTPConnectionClosedException.html">
181 * FTPConnectionClosedException </a>, you must disconnect the connection with
182 * <a href="#disconnect"> disconnect() </a> to properly clean up the
183 * system resources used by FTPClient. Before disconnecting, you may check the
184 * last reply code and text with
185 * <a href="org.apache.commons.net.ftp.FTP.html#getReplyCode"> getReplyCode </a>,
186 * <a href="org.apache.commons.net.ftp.FTP.html#getReplyString"> getReplyString </a>,
187 * and
188 * <a href="org.apache.commons.net.ftp.FTP.html#getReplyStrings"> getReplyStrings</a>.
189 * You may avoid server disconnections while the client is idle by
190 * periodicaly sending NOOP commands to the server.
191 * <p>
192 * Rather than list it separately for each method, we mention here that
193 * every method communicating with the server and throwing an IOException
194 * can also throw a
195 * <a href="org.apache.commons.net.MalformedServerReplyException.html">
196 * MalformedServerReplyException </a>, which is a subclass
197 * of IOException. A MalformedServerReplyException will be thrown when
198 * the reply received from the server deviates enough from the protocol
199 * specification that it cannot be interpreted in a useful manner despite
200 * attempts to be as lenient as possible.
201 * <p>
202 * <p>
203 * @author Daniel F. Savarese
204 * @see FTP
205 * @see FTPConnectionClosedException
206 * @see DefaultFTPFileListParser
207 * @see org.apache.commons.net.MalformedServerReplyException
208 ***/
209
210 public class FTPClient extends FTP
211 {
212 /****
213 * A constant indicating the FTP session is expecting all transfers
214 * to occur between the client (local) and server and that the server
215 * should connect to the client's data port to initiate a data transfer.
216 * This is the default data connection mode when and FTPClient instance
217 * is created.
218 ***/
219 public static final int ACTIVE_LOCAL_DATA_CONNECTION_MODE = 0;
220 /****
221 * A constant indicating the FTP session is expecting all transfers
222 * to occur between two remote servers and that the server
223 * the client is connected to should connect to the other server's
224 * data port to initiate a data transfer.
225 ***/
226 public static final int ACTIVE_REMOTE_DATA_CONNECTION_MODE = 1;
227 /****
228 * A constant indicating the FTP session is expecting all transfers
229 * to occur between the client (local) and server and that the server
230 * is in passive mode, requiring the client to connect to the
231 * server's data port to initiate a transfer.
232 ***/
233 public static final int PASSIVE_LOCAL_DATA_CONNECTION_MODE = 2;
234 /****
235 * A constant indicating the FTP session is expecting all transfers
236 * to occur between two remote servers and that the server
237 * the client is connected to is in passive mode, requiring the other
238 * server to connect to the first server's data port to initiate a data
239 * transfer.
240 ***/
241 public static final int PASSIVE_REMOTE_DATA_CONNECTION_MODE = 3;
242
243 private int __dataConnectionMode, __dataTimeout;
244 private int __passivePort;
245 private String __passiveHost;
246 private int __fileType, __fileFormat, __fileStructure, __fileTransferMode;
247 private boolean __remoteVerificationEnabled;
248 private FTPFileListParser __fileListParser;
249 private long __restartOffset;
250
251 /****
252 * Default FTPClient constructor. Creates a new FTPClient instance
253 * with the data connection mode set to
254 * <code> ACTIVE_LOCAL_DATA_CONNECTION_MODE </code>, the file type
255 * set to <code> FTP.ASCII_FILE_TYPE </code>, the
256 * file format set to <code> FTP.NON_PRINT_TEXT_FORMAT </code>,
257 * the file structure set to <code> FTP.FILE_STRUCTURE </code>, and
258 * the transfer mode set to <code> FTP.STREAM_TRANSFER_MODE </code>.
259 ***/
260 public FTPClient()
261 {
262 __initDefaults();
263 __fileListParser = new DefaultFTPFileListParser();
264 __dataTimeout = -1;
265 __remoteVerificationEnabled = true;
266 }
267
268
269 private void __initDefaults()
270 {
271 __dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
272 __passiveHost = null;
273 __passivePort = -1;
274 __fileType = FTP.ASCII_FILE_TYPE;
275 __fileStructure = FTP.FILE_STRUCTURE;
276 __fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
277 __fileTransferMode = FTP.STREAM_TRANSFER_MODE;
278 }
279
280 private String __parsePathname(String reply)
281 {
282 int begin, end;
283
284 begin = reply.indexOf('"') + 1;
285 end = reply.indexOf('"', begin);
286
287 return reply.substring(begin, end);
288 }
289
290
291 private void __parsePassiveModeReply(String reply)
292 throws MalformedServerReplyException
293 {
294 int i, index, lastIndex;
295 String octet1, octet2;
296 StringBuffer host;
297
298 reply = reply.substring(reply.indexOf('(') + 1,
299 reply.indexOf(')')).trim();
300
301 host = new StringBuffer(24);
302 lastIndex = 0;
303 index = reply.indexOf(',');
304 host.append(reply.substring(lastIndex, index));
305
306 for (i = 0; i < 3; i++)
307 {
308 host.append('.');
309 lastIndex = index + 1;
310 index = reply.indexOf(',', lastIndex);
311 host.append(reply.substring(lastIndex, index));
312 }
313
314 lastIndex = index + 1;
315 index = reply.indexOf(',', lastIndex);
316
317 octet1 = reply.substring(lastIndex, index);
318 octet2 = reply.substring(index + 1);
319
320 // index and lastIndex now used as temporaries
321 try
322 {
323 index = Integer.parseInt(octet1);
324 lastIndex = Integer.parseInt(octet2);
325 }
326 catch (NumberFormatException e)
327 {
328 throw new MalformedServerReplyException(
329 "Could not parse passive host information.\nServer Reply: " + reply);
330 }
331
332 index <<= 8;
333 index |= lastIndex;
334
335 __passiveHost = host.toString();
336 __passivePort = index;
337 }
338
339
340 // null arg if no arg
341 protected Socket __openDataConnection(int command, String arg)
342 throws IOException
343 {
344 Socket socket;
345
346 if (__dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE &&
347 __dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE)
348 return null;
349
350 if (__dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE)
351 {
352 ServerSocket server;
353 server = _socketFactory_.createServerSocket(0, 1, getLocalAddress());
354
355 if (!FTPReply.isPositiveCompletion(port(getLocalAddress(),
356 server.getLocalPort())))
357 {
358 server.close();
359 return null;
360 }
361
362 if ((__restartOffset > 0) && !restart(__restartOffset))
363 {
364 server.close();
365 return null;
366 }
367
368 if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
369 {
370 server.close();
371 return null;
372 }
373
374 // For now, let's just use the data timeout value for waiting for
375 // the data connection. It may be desirable to let this be a
376 // separately configurable value. In any case, we really want
377 // to allow preventing the accept from blocking indefinitely.
378 if (__dataTimeout >= 0)
379 server.setSoTimeout(__dataTimeout);
380 socket = server.accept();
381 server.close();
382 }
383 else
384 { // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE
385
386 if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
387 return null;
388
389 __parsePassiveModeReply((String)_replyLines.elementAt(0));
390
391 socket = _socketFactory_.createSocket(__passiveHost, __passivePort);
392
393 if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
394 {
395 socket.close();
396 return null;
397 }
398 }
399
400 if (__remoteVerificationEnabled && !verifyRemote(socket))
401 {
402 InetAddress host1, host2;
403
404 host1 = socket.getInetAddress();
405 host2 = getRemoteAddress();
406
407 socket.close();
408
409 throw new IOException(
410 "Host attempting data connection " + host1.getHostAddress() +
411 " is not same as server " + host2.getHostAddress());
412 }
413
414 if (__dataTimeout >= 0)
415 socket.setSoTimeout(__dataTimeout);
416
417 return socket;
418 }
419
420
421 private boolean __storeFile(int command, String remote, InputStream local)
422 throws IOException
423 {
424 OutputStream output;
425 Socket socket;
426
427 if ((socket = __openDataConnection(command, remote)) == null)
428 return false;
429
430 output = new BufferedOutputStream(socket.getOutputStream());
431 if (__fileType == ASCII_FILE_TYPE)
432 output = new ToNetASCIIOutputStream(output);
433 // Treat everything else as binary for now
434 try
435 {
436 Util.copyStream(local, output);
437 }
438 catch (IOException e)
439 {
440 try
441 {
442 socket.close();
443 }
444 catch (IOException f)
445 {}
446 throw e;
447 }
448 output.close();
449 socket.close();
450 return completePendingCommand();
451 }
452
453 private OutputStream __storeFileStream(int command, String remote)
454 throws IOException
455 {
456 OutputStream output;
457 Socket socket;
458
459 if ((socket = __openDataConnection(command, remote)) == null)
460 return null;
461
462 output = socket.getOutputStream();
463 if (__fileType == ASCII_FILE_TYPE)
464 output = new ToNetASCIIOutputStream(output);
465 return new org.apache.commons.net.io.SocketOutputStream(socket, output);
466 }
467
468
469 protected void _connectAction_() throws IOException
470 {
471 super._connectAction_();
472 __initDefaults();
473 }
474
475
476 /****
477 * Sets the timeout in milliseconds to use when reading from the
478 * data connection. This timeout will be set immediately after
479 * opening the data connection.
480 * <p>
481 * @param timeout The default timeout in milliseconds that is used when
482 * opening a data connection socket.
483 ***/
484 public void setDataTimeout(int timeout)
485 {
486 __dataTimeout = timeout;
487 }
488
489
490 /****
491 * Closes the connection to the FTP server and restores
492 * connection parameters to the default values.
493 * <p>
494 * @exception IOException If an error occurs while disconnecting.
495 ***/
496 public void disconnect() throws IOException
497 {
498 super.disconnect();
499 __initDefaults();
500 }
501
502
503 /****
504 * Enable or disable verification that the remote host taking part
505 * of a data connection is the same as the host to which the control
506 * connection is attached. The default is for verification to be
507 * enabled. You may set this value at any time, whether the
508 * FTPClient is currently connected or not.
509 * <p>
510 * @param enable True to enable verification, false to disable verification.
511 ***/
512 public void setRemoteVerificationEnabled(boolean enable)
513 {
514 __remoteVerificationEnabled = enable;
515 }
516
517 /****
518 * Return whether or not verification of the remote host participating
519 * in data connections is enabled. The default behavior is for
520 * verification to be enabled.
521 * <p>
522 * @return True if verification is enabled, false if not.
523 ***/
524 public boolean isRemoteVerificationEnabled()
525 {
526 return __remoteVerificationEnabled;
527 }
528
529 /****
530 * Login to the FTP server using the provided username and password.
531 * <p>
532 * @param username The username to login under.
533 * @param password The password to use.
534 * @return True if successfully completed, false if not.
535 * @exception FTPConnectionClosedException
536 * If the FTP server prematurely closes the connection as a result
537 * of the client being idle or some other reason causing the server
538 * to send FTP reply code 421. This exception may be caught either
539 * as an IOException or independently as itself.
540 * @exception IOException If an I/O error occurs while either sending a
541 * command to the server or receiving a reply from the server.
542 ***/
543 public boolean login(String username, String password) throws IOException
544 {
545 user(username);
546
547 if (FTPReply.isPositiveCompletion(_replyCode))
548 return true;
549
550 // If we get here, we either have an error code, or an intermmediate
551 // reply requesting password.
552 if (!FTPReply.isPositiveIntermediate(_replyCode))
553 return false;
554
555 return FTPReply.isPositiveCompletion(pass(password));
556 }
557
558
559 /****
560 * Login to the FTP server using the provided username, password,
561 * and account. If no account is required by the server, only
562 * the username and password, the account information is not used.
563 * <p>
564 * @param username The username to login under.
565 * @param password The password to use.
566 * @param account The account to use.
567 * @return True if successfully completed, false if not.
568 * @exception FTPConnectionClosedException
569 * If the FTP server prematurely closes the connection as a result
570 * of the client being idle or some other reason causing the server
571 * to send FTP reply code 421. This exception may be caught either
572 * as an IOException or independently as itself.
573 * @exception IOException If an I/O error occurs while either sending a
574 * command to the server or receiving a reply from the server.
575 ***/
576 public boolean login(String username, String password, String account)
577 throws IOException
578 {
579 user(username);
580
581 if (FTPReply.isPositiveCompletion(_replyCode))
582 return true;
583
584 // If we get here, we either have an error code, or an intermmediate
585 // reply requesting password.
586 if (!FTPReply.isPositiveIntermediate(_replyCode))
587 return false;
588
589 pass(password);
590
591 if (FTPReply.isPositiveCompletion(_replyCode))
592 return true;
593
594 if (!FTPReply.isPositiveIntermediate(_replyCode))
595 return false;
596
597 return FTPReply.isPositiveCompletion(acct(account));
598 }
599
600 /****
601 * Logout of the FTP server by sending the QUIT command.
602 * <p>
603 * @return True if successfully completed, false if not.
604 * @exception FTPConnectionClosedException
605 * If the FTP server prematurely closes the connection as a result
606 * of the client being idle or some other reason causing the server
607 * to send FTP reply code 421. This exception may be caught either
608 * as an IOException or independently as itself.
609 * @exception IOException If an I/O error occurs while either sending a
610 * command to the server or receiving a reply from the server.
611 ***/
612 public boolean logout() throws IOException
613 {
614 return FTPReply.isPositiveCompletion(quit());
615 }
616
617
618 /****
619 * Change the current working directory of the FTP session.
620 * <p>
621 * @param pathname The new current working directory.
622 * @return True if successfully completed, false if not.
623 * @exception FTPConnectionClosedException
624 * If the FTP server prematurely closes the connection as a result
625 * of the client being idle or some other reason causing the server
626 * to send FTP reply code 421. This exception may be caught either
627 * as an IOException or independently as itself.
628 * @exception IOException If an I/O error occurs while either sending a
629 * command to the server or receiving a reply from the server.
630 ***/
631 public boolean changeWorkingDirectory(String pathname) throws IOException
632 {
633 return FTPReply.isPositiveCompletion(cwd(pathname));
634 }
635
636
637 /****
638 * Change to the parent directory of the current working directory.
639 * <p>
640 * @return True if successfully completed, false if not.
641 * @exception FTPConnectionClosedException
642 * If the FTP server prematurely closes the connection as a result
643 * of the client being idle or some other reason causing the server
644 * to send FTP reply code 421. This exception may be caught either
645 * as an IOException or independently as itself.
646 * @exception IOException If an I/O error occurs while either sending a
647 * command to the server or receiving a reply from the server.
648 ***/
649 public boolean changeToParentDirectory() throws IOException
650 {
651 return FTPReply.isPositiveCompletion(cdup());
652 }
653
654
655 /****
656 * Issue the FTP SMNT command.
657 * <p>
658 * @param pathname The pathname to mount.
659 * @return True if successfully completed, false if not.
660 * @exception FTPConnectionClosedException
661 * If the FTP server prematurely closes the connection as a result
662 * of the client being idle or some other reason causing the server
663 * to send FTP reply code 421. This exception may be caught either
664 * as an IOException or independently as itself.
665 * @exception IOException If an I/O error occurs while either sending a
666 * command to the server or receiving a reply from the server.
667 ***/
668 public boolean structureMount(String pathname) throws IOException
669 {
670 return FTPReply.isPositiveCompletion(smnt(pathname));
671 }
672
673 /****
674 * Reinitialize the FTP session. Not all FTP servers support this
675 * command, which issues the FTP REIN command.
676 * <p>
677 * @return True if successfully completed, false if not.
678 * @exception FTPConnectionClosedException
679 * If the FTP server prematurely closes the connection as a result
680 * of the client being idle or some other reason causing the server
681 * to send FTP reply code 421. This exception may be caught either
682 * as an IOException or independently as itself.
683 * @exception IOException If an I/O error occurs while either sending a
684 * command to the server or receiving a reply from the server.
685 ***/
686 boolean reinitialize() throws IOException
687 {
688 rein();
689
690 if (FTPReply.isPositiveCompletion(_replyCode) ||
691 (FTPReply.isPositivePreliminary(_replyCode) &&
692 FTPReply.isPositiveCompletion(getReply())))
693 {
694
695 __initDefaults();
696
697 return true;
698 }
699
700 return false;
701 }
702
703
704 /****
705 * Set the current data connection mode to
706 * <code>ACTIVE_LOCAL_DATA_CONNECTION_MODE</code>. No communication
707 * with the FTP server is conducted, but this causes all future data
708 * transfers to require the FTP server to connect to the client's
709 * data port. Additionally, to accommodate differences between socket
710 * implementations on different platforms, this method causes the
711 * client to issue a PORT command before every data transfer.
712 ***/
713 public void enterLocalActiveMode()
714 {
715 __dataConnectionMode = ACTIVE_LOCAL_DATA_CONNECTION_MODE;
716 __passiveHost = null;
717 __passivePort = -1;
718 }
719
720
721 /****
722 * Set the current data connection mode to
723 * <code> PASSIVE_LOCAL_DATA_CONNECTION_MODE </code>. Use this
724 * method only for data transfers between the client and server.
725 * This method causes a PASV command to be issued to the server
726 * before the opening of every data connection, telling the server to
727 * open a data port to which the client will connect to conduct
728 * data transfers. The FTPClient will stay in
729 * <code> PASSIVE_LOCAL_DATA_CONNECTION_MODE </code> until the
730 * mode is changed by calling some other method such as
731 * <a href="#enterLocalActiveMode"> enterLocalActiveMode() </a>
732 ***/
733 public void enterLocalPassiveMode()
734 {
735 __dataConnectionMode = PASSIVE_LOCAL_DATA_CONNECTION_MODE;
736 // These will be set when just before a data connection is opened
737 // in __openDataConnection()
738 __passiveHost = null;
739 __passivePort = -1;
740 }
741
742
743 /****
744 * Set the current data connection mode to
745 * <code> ACTIVE_REMOTE_DATA_CONNECTION </code>. Use this method only
746 * for server to server data transfers. This method issues a PORT
747 * command to the server, indicating the other server and port to which
748 * it should connect for data transfers. You must call this method
749 * before EVERY server to server transfer attempt. The FTPClient will
750 * NOT automatically continue to issue PORT commands. You also
751 * must remember to call
752 * <a href="#enterLocalActiveMode"> enterLocalActiveMode() </a> if you
753 * wish to return to the normal data connection mode.
754 * <p>
755 * @param host The passive mode server accepting connections for data
756 * transfers.
757 * @param port The passive mode server's data port.
758 * @return True if successfully completed, false if not.
759 * @exception FTPConnectionClosedException
760 * If the FTP server prematurely closes the connection as a result
761 * of the client being idle or some other reason causing the server
762 * to send FTP reply code 421. This exception may be caught either
763 * as an IOException or independently as itself.
764 * @exception IOException If an I/O error occurs while either sending a
765 * command to the server or receiving a reply from the server.
766 ***/
767 public boolean enterRemoteActiveMode(InetAddress host, int port)
768 throws IOException
769 {
770 if (FTPReply.isPositiveCompletion(port(host, port)))
771 {
772 __dataConnectionMode = ACTIVE_REMOTE_DATA_CONNECTION_MODE;
773 __passiveHost = null;
774 __passivePort = -1;
775 return true;
776 }
777 return false;
778 }
779
780 /****
781 * Set the current data connection mode to
782 * <code> PASSIVE_REMOTE_DATA_CONNECTION_MODE </code>. Use this
783 * method only for server to server data transfers.
784 * This method issues a PASV command to the server, telling it to
785 * open a data port to which the active server will connect to conduct
786 * data transfers. You must call this method
787 * before EVERY server to server transfer attempt. The FTPClient will
788 * NOT automatically continue to issue PASV commands. You also
789 * must remember to call
790 * <a href="#enterLocalActiveMode"> enterLocalActiveMode() </a> if you
791 * wish to return to the normal data connection mode.
792 * <p>
793 * @return True if successfully completed, false if not.
794 * @exception FTPConnectionClosedException
795 * If the FTP server prematurely closes the connection as a result
796 * of the client being idle or some other reason causing the server
797 * to send FTP reply code 421. This exception may be caught either
798 * as an IOException or independently as itself.
799 * @exception IOException If an I/O error occurs while either sending a
800 * command to the server or receiving a reply from the server.
801 ***/
802 public boolean enterRemotePassiveMode() throws IOException
803 {
804 if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
805 return false;
806
807 __dataConnectionMode = PASSIVE_REMOTE_DATA_CONNECTION_MODE;
808 __parsePassiveModeReply((String)_replyLines.elementAt(0));
809
810 return true;
811 }
812
813 /****
814 * Returns the hostname or IP address (in the form of a string) returned
815 * by the server when entering passive mode. If not in passive mode,
816 * returns null. This method only returns a valid value AFTER a
817 * data connection has been opened after a call to
818 * <a href="#enterLocalPassiveMode">enterLocalPassiveMode()</a>.
819 * This is because FTPClient sends a PASV command to the server only
820 * just before opening a data connection, and not when you call
821 * <a href="#enterLocalPassiveMode">enterLocalPassiveMode()</a>.
822 * <p>
823 * @return The passive host name if in passive mode, otherwise null.
824 ***/
825 public String getPassiveHost()
826 {
827 return __passiveHost;
828 }
829
830 /****
831 * If in passive mode, returns the data port of the passive host.
832 * This method only returns a valid value AFTER a
833 * data connection has been opened after a call to
834 * <a href="#enterLocalPassiveMode">enterLocalPassiveMode()</a>.
835 * This is because FTPClient sends a PASV command to the server only
836 * just before opening a data connection, and not when you call
837 * <a href="#enterLocalPassiveMode">enterLocalPassiveMode()</a>.
838 * <p>
839 * @return The data port of the passive server. If not in passive
840 * mode, undefined.
841 ***/
842 public int getPassivePort()
843 {
844 return __passivePort;
845 }
846
847
848 /****
849 * Returns the current data connection mode (one of the
850 * <code> _DATA_CONNECTION_MODE </code> constants.
851 * <p>
852 * @return The current data connection mode (one of the
853 * <code> _DATA_CONNECTION_MODE </code> constants.
854 ***/
855 public int getDataConnectionMode()
856 {
857 return __dataConnectionMode;
858 }
859
860
861 /****
862 * Sets the file type to be transferred. This should be one of
863 * <code> FTP.ASCII_FILE_TYPE </code>, <code> FTP.IMAGE_FILE_TYPE </code>,
864 * etc. The file type only needs to be set when you want to change the
865 * type. After changing it, the new type stays in effect until you change
866 * it again. The default file type is <code> FTP.ASCII_FILE_TYPE </code>
867 * if this method is never called.
868 * <p>
869 * @param fileType The <code> _FILE_TYPE </code> constant indcating the
870 * type of file.
871 * @return True if successfully completed, false if not.
872 * @exception FTPConnectionClosedException
873 * If the FTP server prematurely closes the connection as a result
874 * of the client being idle or some other reason causing the server
875 * to send FTP reply code 421. This exception may be caught either
876 * as an IOException or independently as itself.
877 * @exception IOException If an I/O error occurs while either sending a
878 * command to the server or receiving a reply from the server.
879 ***/
880 public boolean setFileType(int fileType) throws IOException
881 {
882 if (FTPReply.isPositiveCompletion(type(fileType)))
883 {
884 __fileType = fileType;
885 __fileFormat = FTP.NON_PRINT_TEXT_FORMAT;
886 return true;
887 }
888 return false;
889 }
890
891
892 /****
893 * Sets the file type to be transferred and the format. The type should be
894 * one of <code> FTP.ASCII_FILE_TYPE </code>,
895 * <code> FTP.IMAGE_FILE_TYPE </code>, etc. The file type only needs to
896 * be set when you want to change the type. After changing it, the new
897 * type stays in effect until you change it again. The default file type
898 * is <code> FTP.ASCII_FILE_TYPE </code> if this method is never called.
899 * The format should be one of the FTP class <code> TEXT_FORMAT </code>
900 * constants, or if the type is <code> FTP.LOCAL_FILE_TYPE </code>, the
901 * format should be the byte size for that type. The default format
902 * is <code> FTP.NON_PRINT_TEXT_FORMAT </code> if this method is never
903 * called.
904 * <p>
905 * @param fileType The <code> _FILE_TYPE </code> constant indcating the
906 * type of file.
907 * @param formatOrByteSize The format of the file (one of the
908 * <code>_FORMAT</code> constants. In the case of
909 * <code>LOCAL_FILE_TYPE</code>, the byte size.
910 * <p>
911 * @return True if successfully completed, false if not.
912 * @exception FTPConnectionClosedException
913 * If the FTP server prematurely closes the connection as a result
914 * of the client being idle or some other reason causing the server
915 * to send FTP reply code 421. This exception may be caught either
916 * as an IOException or independently as itself.
917 * @exception IOException If an I/O error occurs while either sending a
918 * command to the server or receiving a reply from the server.
919 ***/
920 public boolean setFileType(int fileType, int formatOrByteSize)
921 throws IOException
922 {
923 if (FTPReply.isPositiveCompletion(type(fileType, formatOrByteSize)))
924 {
925 __fileType = fileType;
926 __fileFormat = formatOrByteSize;
927 return true;
928 }
929 return false;
930 }
931
932
933 /****
934 * Sets the file structure. The default structure is
935 * <code> FTP.FILE_STRUCTURE </code> if this method is never called.
936 * <p>
937 * @param structure The structure of the file (one of the FTP class
938 * <code>_STRUCTURE</code> constants).
939 * @return True if successfully completed, false if not.
940 * @exception FTPConnectionClosedException
941 * If the FTP server prematurely closes the connection as a result
942 * of the client being idle or some other reason causing the server
943 * to send FTP reply code 421. This exception may be caught either
944 * as an IOException or independently as itself.
945 * @exception IOException If an I/O error occurs while either sending a
946 * command to the server or receiving a reply from the server.
947 ***/
948 public boolean setFileStructure(int structure) throws IOException
949 {
950 if (FTPReply.isPositiveCompletion(stru(structure)))
951 {
952 __fileStructure = structure;
953 return true;
954 }
955 return false;
956 }
957
958
959 /****
960 * Sets the transfer mode. The default transfer mode
961 * <code> FTP.STREAM_TRANSFER_MODE </code> if this method is never called.
962 * <p>
963 * @param mode The new transfer mode to use (one of the FTP class
964 * <code>_TRANSFER_MODE</code> constants).
965 * @return True if successfully completed, false if not.
966 * @exception FTPConnectionClosedException
967 * If the FTP server prematurely closes the connection as a result
968 * of the client being idle or some other reason causing the server
969 * to send FTP reply code 421. This exception may be caught either
970 * as an IOException or independently as itself.
971 * @exception IOException If an I/O error occurs while either sending a
972 * command to the server or receiving a reply from the server.
973 ***/
974 public boolean setFileTransferMode(int mode) throws IOException
975 {
976 if (FTPReply.isPositiveCompletion(mode(mode)))
977 {
978 __fileTransferMode = mode;
979 return true;
980 }
981 return false;
982 }
983
984
985 /****
986 * Initiate a server to server file transfer. This method tells the
987 * server to which the client is connected to retrieve a given file from
988 * the other server.
989 * <p>
990 * @param filename The name of the file to retrieve.
991 * @return True if successfully completed, false if not.
992 * @exception FTPConnectionClosedException
993 * If the FTP server prematurely closes the connection as a result
994 * of the client being idle or some other reason causing the server
995 * to send FTP reply code 421. This exception may be caught either
996 * as an IOException or independently as itself.
997 * @exception IOException If an I/O error occurs while either sending a
998 * command to the server or receiving a reply from the server.
999 ***/
1000 public boolean remoteRetrieve(String filename) throws IOException
1001 {
1002 if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
1003 __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
1004 return FTPReply.isPositivePreliminary(retr(filename));
1005 return false;
1006 }
1007
1008
1009 /****
1010 * Initiate a server to server file transfer. This method tells the
1011 * server to which the client is connected to store a file on
1012 * the other server using the given filename. The other server must
1013 * have had a <code> remoteRetrieve </code> issued to it by another
1014 * FTPClient.
1015 * <p>
1016 * @param filename The name to call the file that is to be stored.
1017 * @return True if successfully completed, false if not.
1018 * @exception FTPConnectionClosedException
1019 * If the FTP server prematurely closes the connection as a result
1020 * of the client being idle or some other reason causing the server
1021 * to send FTP reply code 421. This exception may be caught either
1022 * as an IOException or independently as itself.
1023 * @exception IOException If an I/O error occurs while either sending a
1024 * command to the server or receiving a reply from the server.
1025 ***/
1026 public boolean remoteStore(String filename) throws IOException
1027 {
1028 if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
1029 __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
1030 return FTPReply.isPositivePreliminary(stor(filename));
1031 return false;
1032 }
1033
1034
1035 /****
1036 * Initiate a server to server file transfer. This method tells the
1037 * server to which the client is connected to store a file on
1038 * the other server using a unique filename based on the given filename.
1039 * The other server must have had a <code> remoteRetrieve </code> issued
1040 * to it by another FTPClient.
1041 * <p>
1042 * @param filename The name on which to base the filename of the file
1043 * that is to be stored.
1044 * @return True if successfully completed, false if not.
1045 * @exception FTPConnectionClosedException
1046 * If the FTP server prematurely closes the connection as a result
1047 * of the client being idle or some other reason causing the server
1048 * to send FTP reply code 421. This exception may be caught either
1049 * as an IOException or independently as itself.
1050 * @exception IOException If an I/O error occurs while either sending a
1051 * command to the server or receiving a reply from the server.
1052 ***/
1053 public boolean remoteStoreUnique(String filename) throws IOException
1054 {
1055 if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
1056 __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
1057 return FTPReply.isPositivePreliminary(stou(filename));
1058 return false;
1059 }
1060
1061
1062 /****
1063 * Initiate a server to server file transfer. This method tells the
1064 * server to which the client is connected to store a file on
1065 * the other server using a unique filename.
1066 * The other server must have had a <code> remoteRetrieve </code> issued
1067 * to it by another FTPClient. Many FTP servers require that a base
1068 * filename be given from which the unique filename can be derived. For
1069 * those servers use the other version of <code> remoteStoreUnique</code>
1070 * <p>
1071 * @return True if successfully completed, false if not.
1072 * @exception FTPConnectionClosedException
1073 * If the FTP server prematurely closes the connection as a result
1074 * of the client being idle or some other reason causing the server
1075 * to send FTP reply code 421. This exception may be caught either
1076 * as an IOException or independently as itself.
1077 * @exception IOException If an I/O error occurs while either sending a
1078 * command to the server or receiving a reply from the server.
1079 ***/
1080 public boolean remoteStoreUnique() throws IOException
1081 {
1082 if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
1083 __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
1084 return FTPReply.isPositivePreliminary(stou());
1085 return false;
1086 }
1087
1088 // For server to server transfers
1089 /****
1090 * Initiate a server to server file transfer. This method tells the
1091 * server to which the client is connected to append to a given file on
1092 * the other server. The other server must have had a
1093 * <code> remoteRetrieve </code> issued to it by another FTPClient.
1094 * <p>
1095 * @param filename The name of the file to be appended to, or if the
1096 * file does not exist, the name to call the file being stored.
1097 * <p>
1098 * @return True if successfully completed, false if not.
1099 * @exception FTPConnectionClosedException
1100 * If the FTP server prematurely closes the connection as a result
1101 * of the client being idle or some other reason causing the server
1102 * to send FTP reply code 421. This exception may be caught either
1103 * as an IOException or independently as itself.
1104 * @exception IOException If an I/O error occurs while either sending a
1105 * command to the server or receiving a reply from the server.
1106 ***/
1107 public boolean remoteAppend(String filename) throws IOException
1108 {
1109 if (__dataConnectionMode == ACTIVE_REMOTE_DATA_CONNECTION_MODE ||
1110 __dataConnectionMode == PASSIVE_REMOTE_DATA_CONNECTION_MODE)
1111 return FTPReply.isPositivePreliminary(stor(filename));
1112 return false;
1113 }
1114
1115 /****
1116 * There are a few FTPClient methods that do not complete the
1117 * entire sequence of FTP commands to complete a transaction. These
1118 * commands require some action by the programmer after the reception
1119 * of a positive intermediate command. After the programmer's code
1120 * completes its actions, it must call this method to receive
1121 * the completion reply from the server and verify the success of the
1122 * entire transaction.
1123 * <p>
1124 * For example,
1125 * <pre>
1126 * InputStream input;
1127 * OutputStream output;
1128 * input = new FileInputStream("foobaz.txt");
1129 * output = ftp.storeFileStream("foobar.txt")
1130 * if(!FTPReply.isPositiveIntermediate(ftp.getReplyCode())) {
1131 * input.close();
1132 * output.close();
1133 * ftp.logout();
1134 * ftp.disconnect();
1135 * System.err.println("File transfer failed.");
1136 * System.exit(1);
1137 * }
1138 * Util.copyStream(input, output);
1139 * input.close();
1140 * output.close();
1141 * // Must call completePendingCommand() to finish command.
1142 * if(!ftp.completePendingCommand()) {
1143 * ftp.logout();
1144 * ftp.disconnect();
1145 * System.err.println("File transfer failed.");
1146 * System.exit(1);
1147 * }
1148 * </pre>
1149 * <p>
1150 * @return True if successfully completed, false if not.
1151 * @exception FTPConnectionClosedException
1152 * If the FTP server prematurely closes the connection as a result
1153 * of the client being idle or some other reason causing the server
1154 * to send FTP reply code 421. This exception may be caught either
1155 * as an IOException or independently as itself.
1156 * @exception IOException If an I/O error occurs while either sending a
1157 * command to the server or receiving a reply from the server.
1158 ***/
1159 public boolean completePendingCommand() throws IOException
1160 {
1161 return FTPReply.isPositiveCompletion(getReply());
1162 }
1163
1164
1165 /****
1166 * Retrieves a named file from the server and writes it to the given
1167 * OutputStream. This method does NOT close the given OutputStream.
1168 * If the current file type is ASCII, line separators in the file are
1169 * converted to the local representation.
1170 * <p>
1171 * @param remote The name of the remote file.
1172 * @param local The local OutputStream to which to write the file.
1173 * @return True if successfully completed, false if not.
1174 * @exception FTPConnectionClosedException
1175 * If the FTP server prematurely closes the connection as a result
1176 * of the client being idle or some other reason causing the server
1177 * to send FTP reply code 421. This exception may be caught either
1178 * as an IOException or independently as itself.
1179 * @exception CopyStreamException If an I/O error occurs while actually
1180 * transferring the file. The CopyStreamException allows you to
1181 * determine the number of bytes transferred and the IOException
1182 * causing the error. This exception may be caught either
1183 * as an IOException or independently as itself.
1184 * @exception IOException If an I/O error occurs while either sending a
1185 * command to the server or receiving a reply from the server.
1186 ***/
1187 public boolean retrieveFile(String remote, OutputStream local)
1188 throws IOException
1189 {
1190 InputStream input;
1191 Socket socket;
1192
1193 if ((socket = __openDataConnection(FTPCommand.RETR, remote)) == null)
1194 return false;
1195
1196 input = new BufferedInputStream(socket.getInputStream());
1197 if (__fileType == ASCII_FILE_TYPE)
1198 input = new FromNetASCIIInputStream(input);
1199 // Treat everything else as binary for now
1200 try
1201 {
1202 Util.copyStream(input, local);
1203 }
1204 catch (IOException e)
1205 {
1206 try
1207 {
1208 socket.close();
1209 }
1210 catch (IOException f)
1211 {}
1212 throw e;
1213 }
1214 socket.close();
1215 return completePendingCommand();
1216 }
1217
1218 /****
1219 * Returns an InputStream from which a named file from the server
1220 * can be read. If the current file type is ASCII, the returned
1221 * InputStream will convert line separators in the file to
1222 * the local representation. You must close the InputStream when you
1223 * finish reading from it. The InputStream itself will take care of
1224 * closing the parent data connection socket upon being closed. To
1225 * finalize the file transfer you must call
1226 * <a href="#completePendingCommand"> completePendingCommand </a> and
1227 * check its return value to verify success.
1228 * <p>
1229 * @param remote The name of the remote file.
1230 * @return An InputStream from which the remote file can be read. If
1231 * the data connection cannot be opened (e.g., the file does not
1232 * exist), null is returned (in which case you may check the reply
1233 * code to determine the exact reason for failure).
1234 * @exception FTPConnectionClosedException
1235 * If the FTP server prematurely closes the connection as a result
1236 * of the client being idle or some other reason causing the server
1237 * to send FTP reply code 421. This exception may be caught either
1238 * as an IOException or independently as itself.
1239 * @exception IOException If an I/O error occurs while either sending a
1240 * command to the server or receiving a reply from the server.
1241 ***/
1242 public InputStream retrieveFileStream(String remote) throws IOException
1243 {
1244 InputStream input;
1245 Socket socket;
1246
1247 if ((socket = __openDataConnection(FTPCommand.RETR, remote)) == null)
1248 return null;
1249
1250 input = socket.getInputStream();
1251 if (__fileType == ASCII_FILE_TYPE)
1252 input = new FromNetASCIIInputStream(input);
1253 return new org.apache.commons.net.io.SocketInputStream(socket, input);
1254 }
1255
1256
1257 /****
1258 * Stores a file on the server using the given name and taking input
1259 * from the given InputStream. This method does NOT close the given
1260 * InputStream. If the current file type is ASCII, line separators in
1261 * the file are transparently converted to the NETASCII format (i.e.,
1262 * you should not attempt to create a special InputStream to do this).
1263 * <p>
1264 * @param remote The name to give the remote file.
1265 * @param local The local InputStream from which to read the file.
1266 * @return True if successfully completed, false if not.
1267 * @exception FTPConnectionClosedException
1268 * If the FTP server prematurely closes the connection as a result
1269 * of the client being idle or some other reason causing the server
1270 * to send FTP reply code 421. This exception may be caught either
1271 * as an IOException or independently as itself.
1272 * @exception CopyStreamException If an I/O error occurs while actually
1273 * transferring the file. The CopyStreamException allows you to
1274 * determine the number of bytes transferred and the IOException
1275 * causing the error. This exception may be caught either
1276 * as an IOException or independently as itself.
1277 * @exception IOException If an I/O error occurs while either sending a
1278 * command to the server or receiving a reply from the server.
1279 ***/
1280 public boolean storeFile(String remote, InputStream local)
1281 throws IOException
1282 {
1283 return __storeFile(FTPCommand.STOR, remote, local);
1284 }
1285
1286
1287 /****
1288 * Returns an OutputStream through which data can be written to store
1289 * a file on the server using the given name. If the current file type
1290 * is ASCII, the returned OutputStream will convert line separators in
1291 * the file to the NETASCII format (i.e., you should not attempt to
1292 * create a special OutputStream to do this). You must close the
1293 * OutputStream when you finish writing to it. The OutputStream itself
1294 * will take care of closing the parent data connection socket upon being
1295 * closed. To finalize the file transfer you must call
1296 * <a href="#completePendingCommand"> completePendingCommand </a> and
1297 * check its return value to verify success.
1298 * <p>
1299 * @param remote The name to give the remote file.
1300 * @return An OutputStream through which the remote file can be written. If
1301 * the data connection cannot be opened (e.g., the file does not
1302 * exist), null is returned (in which case you may check the reply
1303 * code to determine the exact reason for failure).
1304 * @exception FTPConnectionClosedException
1305 * If the FTP server prematurely closes the connection as a result
1306 * of the client being idle or some other reason causing the server
1307 * to send FTP reply code 421. This exception may be caught either
1308 * as an IOException or independently as itself.
1309 * @exception IOException If an I/O error occurs while either sending a
1310 * command to the server or receiving a reply from the server.
1311 ***/
1312 public OutputStream storeFileStream(String remote) throws IOException
1313 {
1314 return __storeFileStream(FTPCommand.STOR, remote);
1315 }
1316
1317 /****
1318 * Appends to a file on the server with the given name, taking input
1319 * from the given InputStream. This method does NOT close the given
1320 * InputStream. If the current file type is ASCII, line separators in
1321 * the file are transparently converted to the NETASCII format (i.e.,
1322 * you should not attempt to create a special InputStream to do this).
1323 * <p>
1324 * @param remote The name of the remote file.
1325 * @param local The local InputStream from which to read the data to
1326 * be appended to the remote file.
1327 * @return True if successfully completed, false if not.
1328 * @exception FTPConnectionClosedException
1329 * If the FTP server prematurely closes the connection as a result
1330 * of the client being idle or some other reason causing the server
1331 * to send FTP reply code 421. This exception may be caught either
1332 * as an IOException or independently as itself.
1333 * @exception CopyStreamException If an I/O error occurs while actually
1334 * transferring the file. The CopyStreamException allows you to
1335 * determine the number of bytes transferred and the IOException
1336 * causing the error. This exception may be caught either
1337 * as an IOException or independently as itself.
1338 * @exception IOException If an I/O error occurs while either sending a
1339 * command to the server or receiving a reply from the server.
1340 ***/
1341 public boolean appendFile(String remote, InputStream local)
1342 throws IOException
1343 {
1344 return __storeFile(FTPCommand.APPE, remote, local);
1345 }
1346
1347 /****
1348 * Returns an OutputStream through which data can be written to append
1349 * to a file on the server with the given name. If the current file type
1350 * is ASCII, the returned OutputStream will convert line separators in
1351 * the file to the NETASCII format (i.e., you should not attempt to
1352 * create a special OutputStream to do this). You must close the
1353 * OutputStream when you finish writing to it. The OutputStream itself
1354 * will take care of closing the parent data connection socket upon being
1355 * closed. To finalize the file transfer you must call
1356 * <a href="#completePendingCommand"> completePendingCommand </a> and
1357 * check its return value to verify success.
1358 * <p>
1359 * @param remote The name of the remote file.
1360 * @return An OutputStream through which the remote file can be appended.
1361 * If the data connection cannot be opened (e.g., the file does not
1362 * exist), null is returned (in which case you may check the reply
1363 * code to determine the exact reason for failure).
1364 * @exception FTPConnectionClosedException
1365 * If the FTP server prematurely closes the connection as a result
1366 * of the client being idle or some other reason causing the server
1367 * to send FTP reply code 421. This exception may be caught either
1368 * as an IOException or independently as itself.
1369 * @exception IOException If an I/O error occurs while either sending a
1370 * command to the server or receiving a reply from the server.
1371 ***/
1372 public OutputStream appendFileStream(String remote) throws IOException
1373 {
1374 return __storeFileStream(FTPCommand.APPE, remote);
1375 }
1376
1377 /****
1378 * Stores a file on the server using a unique name derived from the
1379 * given name and taking input
1380 * from the given InputStream. This method does NOT close the given
1381 * InputStream. If the current file type is ASCII, line separators in
1382 * the file are transparently converted to the NETASCII format (i.e.,
1383 * you should not attempt to create a special InputStream to do this).
1384 * <p>
1385 * @param remote The name on which to base the unique name given to
1386 * the remote file.
1387 * @param local The local InputStream from which to read the file.
1388 * @return True if successfully completed, false if not.
1389 * @exception FTPConnectionClosedException
1390 * If the FTP server prematurely closes the connection as a result
1391 * of the client being idle or some other reason causing the server
1392 * to send FTP reply code 421. This exception may be caught either
1393 * as an IOException or independently as itself.
1394 * @exception CopyStreamException If an I/O error occurs while actually
1395 * transferring the file. The CopyStreamException allows you to
1396 * determine the number of bytes transferred and the IOException
1397 * causing the error. This exception may be caught either
1398 * as an IOException or independently as itself.
1399 * @exception IOException If an I/O error occurs while either sending a
1400 * command to the server or receiving a reply from the server.
1401 ***/
1402 public boolean storeUniqueFile(String remote, InputStream local)
1403 throws IOException
1404 {
1405 return __storeFile(FTPCommand.STOU, remote, local);
1406 }
1407
1408
1409 /****
1410 * Returns an OutputStream through which data can be written to store
1411 * a file on the server using a unique name derived from the given name.
1412 * If the current file type
1413 * is ASCII, the returned OutputStream will convert line separators in
1414 * the file to the NETASCII format (i.e., you should not attempt to
1415 * create a special OutputStream to do this). You must close the
1416 * OutputStream when you finish writing to it. The OutputStream itself
1417 * will take care of closing the parent data connection socket upon being
1418 * closed. To finalize the file transfer you must call
1419 * <a href="#completePendingCommand"> completePendingCommand </a> and
1420 * check its return value to verify success.
1421 * <p>
1422 * @param remote The name on which to base the unique name given to
1423 * the remote file.
1424 * @return An OutputStream through which the remote file can be written. If
1425 * the data connection cannot be opened (e.g., the file does not
1426 * exist), null is returned (in which case you may check the reply
1427 * code to determine the exact reason for failure).
1428 * @exception FTPConnectionClosedException
1429 * If the FTP server prematurely closes the connection as a result
1430 * of the client being idle or some other reason causing the server
1431 * to send FTP reply code 421. This exception may be caught either
1432 * as an IOException or independently as itself.
1433 * @exception IOException If an I/O error occurs while either sending a
1434 * command to the server or receiving a reply from the server.
1435 ***/
1436 public OutputStream storeUniqueFileStream(String remote) throws IOException
1437 {
1438 return __storeFileStream(FTPCommand.STOU, remote);
1439 }
1440
1441 /****
1442 * Stores a file on the server using a unique name assigned by the
1443 * server and taking input from the given InputStream. This method does
1444 * NOT close the given
1445 * InputStream. If the current file type is ASCII, line separators in
1446 * the file are transparently converted to the NETASCII format (i.e.,
1447 * you should not attempt to create a special InputStream to do this).
1448 * <p>
1449 * @param remote The name to give the remote file.
1450 * @param local The local InputStream from which to read the file.
1451 * @return True if successfully completed, false if not.
1452 * @exception FTPConnectionClosedException
1453 * If the FTP server prematurely closes the connection as a result
1454 * of the client being idle or some other reason causing the server
1455 * to send FTP reply code 421. This exception may be caught either
1456 * as an IOException or independently as itself.
1457 * @exception CopyStreamException If an I/O error occurs while actually
1458 * transferring the file. The CopyStreamException allows you to
1459 * determine the number of bytes transferred and the IOException
1460 * causing the error. This exception may be caught either
1461 * as an IOException or independently as itself.
1462 * @exception IOException If an I/O error occurs while either sending a
1463 * command to the server or receiving a reply from the server.
1464 ***/
1465 public boolean storeUniqueFile(InputStream local) throws IOException
1466 {
1467 return __storeFile(FTPCommand.STOU, null, local);
1468 }
1469
1470 /****
1471 * Returns an OutputStream through which data can be written to store
1472 * a file on the server using a unique name assigned by the server.
1473 * If the current file type
1474 * is ASCII, the returned OutputStream will convert line separators in
1475 * the file to the NETASCII format (i.e., you should not attempt to
1476 * create a special OutputStream to do this). You must close the
1477 * OutputStream when you finish writing to it. The OutputStream itself
1478 * will take care of closing the parent data connection socket upon being
1479 * closed. To finalize the file transfer you must call
1480 * <a href="#completePendingCommand"> completePendingCommand </a> and
1481 * check its return value to verify success.
1482 * <p>
1483 * @param remote The name to give the remote file.
1484 * @return An OutputStream through which the remote file can be written. If
1485 * the data connection cannot be opened (e.g., the file does not
1486 * exist), null is returned (in which case you may check the reply
1487 * code to determine the exact reason for failure).
1488 * @exception FTPConnectionClosedException
1489 * If the FTP server prematurely closes the connection as a result
1490 * of the client being idle or some other reason causing the server
1491 * to send FTP reply code 421. This exception may be caught either
1492 * as an IOException or independently as itself.
1493 * @exception IOException If an I/O error occurs while either sending a
1494 * command to the server or receiving a reply from the server.
1495 ***/
1496 public OutputStream storeUniqueFileStream() throws IOException
1497 {
1498 return __storeFileStream(FTPCommand.STOU, null);
1499 }
1500
1501 /****
1502 * Reserve a number of bytes on the server for the next file transfer.
1503 * <p>
1504 * @param bytes The number of bytes which the server should allocate.
1505 * @return True if successfully completed, false if not.
1506 * @exception FTPConnectionClosedException
1507 * If the FTP server prematurely closes the connection as a result
1508 * of the client being idle or some other reason causing the server
1509 * to send FTP reply code 421. This exception may be caught either
1510 * as an IOException or independently as itself.
1511 * @exception IOException If an I/O error occurs while either sending a
1512 * command to the server or receiving a reply from the server.
1513 ***/
1514 public boolean allocate(int bytes) throws IOException
1515 {
1516 return FTPReply.isPositiveCompletion(allo(bytes));
1517 }
1518
1519
1520 /****
1521 * Reserve space on the server for the next file transfer.
1522 * <p>
1523 * @param bytes The number of bytes which the server should allocate.
1524 * @param bytes The size of a file record.
1525 * @return True if successfully completed, false if not.
1526 * @exception FTPConnectionClosedException
1527 * If the FTP server prematurely closes the connection as a result
1528 * of the client being idle or some other reason causing the server
1529 * to send FTP reply code 421. This exception may be caught either
1530 * as an IOException or independently as itself.
1531 * @exception IOException If an I/O error occurs while either sending a
1532 * command to the server or receiving a reply from the server.
1533 ***/
1534 public boolean allocate(int bytes, int recordSize) throws IOException
1535 {
1536 return FTPReply.isPositiveCompletion(allo(bytes, recordSize));
1537 }
1538
1539
1540 /****
1541 * Restart a <code>STREAM_TRANSFER_MODE</code> file transfer starting
1542 * from the given offset. This will only work on FTP servers supporting
1543 * the REST comand for the stream transfer mode. However, most FTP
1544 * servers support this. Any subsequent file transfer will start
1545 * reading or writing the remote file from the indicated offset.
1546 * <p>
1547 * @param offset The offset into the remote file at which to start the
1548 * next file transfer.
1549 * @return True if successfully completed, false if not.
1550 * @exception FTPConnectionClosedException
1551 * If the FTP server prematurely closes the connection as a result
1552 * of the client being idle or some other reason causing the server
1553 * to send FTP reply code 421. This exception may be caught either
1554 * as an IOException or independently as itself.
1555 * @exception IOException If an I/O error occurs while either sending a
1556 * command to the server or receiving a reply from the server.
1557 ***/
1558 private boolean restart(long offset) throws IOException
1559 {
1560 __restartOffset = 0;
1561 return FTPReply.isPositiveIntermediate(rest(Long.toString(offset)));
1562 }
1563
1564 /****
1565 * Sets the restart offset. The restart command is sent to the server
1566 * only before sending the file transfer command. When this is done,
1567 * the restart marker is reset to zero.
1568 * <p>
1569 * @param offset The offset into the remote file at which to start the
1570 * next file transfer. This must be a value greater than or
1571 * equal to zero.
1572 ***/
1573 public void setRestartOffset(long offset)
1574 {
1575 if (offset >= 0)
1576 __restartOffset = offset;
1577 }
1578
1579 /****
1580 * Fetches the restart offset.
1581 * <p>
1582 * @return offset The offset into the remote file at which to start the
1583 * next file transfer.
1584 ***/
1585 public long getRestartOffset()
1586 {
1587 return __restartOffset;
1588 }
1589
1590
1591
1592 /****
1593 * Renames a remote file.
1594 * <p>
1595 * @param from The name of the remote file to rename.
1596 * @param to The new name of the remote file.
1597 * @return True if successfully completed, false if not.
1598 * @exception FTPConnectionClosedException
1599 * If the FTP server prematurely closes the connection as a result
1600 * of the client being idle or some other reason causing the server
1601 * to send FTP reply code 421. This exception may be caught either
1602 * as an IOException or independently as itself.
1603 * @exception IOException If an I/O error occurs while either sending a
1604 * command to the server or receiving a reply from the server.
1605 ***/
1606 public boolean rename(String from, String to) throws IOException
1607 {
1608 if (!FTPReply.isPositiveIntermediate(rnfr(from)))
1609 return false;
1610
1611 return FTPReply.isPositiveCompletion(rnto(to));
1612 }
1613
1614
1615 /****
1616 * Abort a transfer in progress.
1617 * <p>
1618 * @return True if successfully completed, false if not.
1619 * @exception FTPConnectionClosedException
1620 * If the FTP server prematurely closes the connection as a result
1621 * of the client being idle or some other reason causing the server
1622 * to send FTP reply code 421. This exception may be caught either
1623 * as an IOException or independently as itself.
1624 * @exception IOException If an I/O error occurs while either sending a
1625 * command to the server or receiving a reply from the server.
1626 ***/
1627 public boolean abort() throws IOException
1628 {
1629 return FTPReply.isPositiveCompletion(abor());
1630 }
1631
1632 /****
1633 * Deletes a file on the FTP server.
1634 * <p>
1635 * @param pathname The pathname of the file to be deleted.
1636 * @return True if successfully completed, false if not.
1637 * @exception FTPConnectionClosedException
1638 * If the FTP server prematurely closes the connection as a result
1639 * of the client being idle or some other reason causing the server
1640 * to send FTP reply code 421. This exception may be caught either
1641 * as an IOException or independently as itself.
1642 * @exception IOException If an I/O error occurs while either sending a
1643 * command to the server or receiving a reply from the server.
1644 ***/
1645 public boolean deleteFile(String pathname) throws IOException
1646 {
1647 return FTPReply.isPositiveCompletion(dele(pathname));
1648 }
1649
1650
1651 /****
1652 * Removes a directory on the FTP server (if empty).
1653 * <p>
1654 * @param pathname The pathname of the directory to remove.
1655 * @return True if successfully completed, false if not.
1656 * @exception FTPConnectionClosedException
1657 * If the FTP server prematurely closes the connection as a result
1658 * of the client being idle or some other reason causing the server
1659 * to send FTP reply code 421. This exception may be caught either
1660 * as an IOException or independently as itself.
1661 * @exception IOException If an I/O error occurs while either sending a
1662 * command to the server or receiving a reply from the server.
1663 ***/
1664 public boolean removeDirectory(String pathname) throws IOException
1665 {
1666 return FTPReply.isPositiveCompletion(rmd(pathname));
1667 }
1668
1669
1670 /****
1671 * Creates a new subdirectory on the FTP server in the current directory
1672 * (if a relative pathname is given) or where specified (if an absolute
1673 * pathname is given).
1674 * <p>
1675 * @param pathname The pathname of the directory to create.
1676 * @return True if successfully completed, false if not.
1677 * @exception FTPConnectionClosedException
1678 * If the FTP server prematurely closes the connection as a result
1679 * of the client being idle or some other reason causing the server
1680 * to send FTP reply code 421. This exception may be caught either
1681 * as an IOException or independently as itself.
1682 * @exception IOException If an I/O error occurs while either sending a
1683 * command to the server or receiving a reply from the server.
1684 ***/
1685 public boolean makeDirectory(String pathname) throws IOException
1686 {
1687 return FTPReply.isPositiveCompletion(mkd(pathname));
1688 }
1689
1690
1691 /****
1692 * Returns the pathname of the current working directory.
1693 * <p>
1694 * @return The pathname of the current working directory. If it cannot
1695 * be obtained, returns null.
1696 * @exception FTPConnectionClosedException
1697 * If the FTP server prematurely closes the connection as a result
1698 * of the client being idle or some other reason causing the server
1699 * to send FTP reply code 421. This exception may be caught either
1700 * as an IOException or independently as itself.
1701 * @exception IOException If an I/O error occurs while either sending a
1702 * command to the server or receiving a reply from the server.
1703 ***/
1704 public String printWorkingDirectory() throws IOException
1705 {
1706 if (pwd() != FTPReply.PATHNAME_CREATED)
1707 return null;
1708
1709 return __parsePathname((String)_replyLines.elementAt(0));
1710 }
1711
1712
1713 /****
1714 * Send a site specific command.
1715 * <p>
1716 * @param argument The site specific command and arguments.
1717 * @return True if successfully completed, false if not.
1718 * @exception FTPConnectionClosedException
1719 * If the FTP server prematurely closes the connection as a result
1720 * of the client being idle or some other reason causing the server
1721 * to send FTP reply code 421. This exception may be caught either
1722 * as an IOException or independently as itself.
1723 * @exception IOException If an I/O error occurs while either sending a
1724 * command to the server or receiving a reply from the server.
1725 ***/
1726 public boolean sendSiteCommand(String arguments) throws IOException
1727 {
1728 return FTPReply.isPositiveCompletion(site(arguments));
1729 }
1730
1731
1732 /****
1733 * Fetches the system type name from the server and returns the string.
1734 * <p>
1735 * @return The system type name obtained from the server. null if the
1736 * information could not be obtained.
1737 * @exception FTPConnectionClosedException
1738 * If the FTP server prematurely closes the connection as a result
1739 * of the client being idle or some other reason causing the server
1740 * to send FTP reply code 421. This exception may be caught either
1741 * as an IOException or independently as itself.
1742 * @exception IOException If an I/O error occurs while either sending a
1743 * command to the server or receiving a reply from the server.
1744 ***/
1745 public String getSystemName() throws IOException
1746 {
1747 if (syst() == FTPReply.NAME_SYSTEM_TYPE)
1748 return ((String)_replyLines.elementAt(0)).substring(4);
1749
1750 return null;
1751 }
1752
1753
1754 /****
1755 * Fetches the system help information from the server and returns the
1756 * full string.
1757 * <p>
1758 * @return The system help string obtained from the server. null if the
1759 * information could not be obtained.
1760 * @exception FTPConnectionClosedException
1761 * If the FTP server prematurely closes the connection as a result
1762 * of the client being idle or some other reason causing the server
1763 * to send FTP reply code 421. This exception may be caught either
1764 * as an IOException or independently as itself.
1765 * @exception IOException If an I/O error occurs while either sending a
1766 * command to the server or receiving a reply from the server.
1767 ***/
1768 public String listHelp() throws IOException
1769 {
1770 if (FTPReply.isPositiveCompletion(help()))
1771 return getReplyString();
1772 return null;
1773 }
1774
1775
1776 /****
1777 * Fetches the help information for a given command from the server and
1778 * returns the full string.
1779 * <p>
1780 * @param The command on which to ask for help.
1781 * @return The command help string obtained from the server. null if the
1782 * information could not be obtained.
1783 * @exception FTPConnectionClosedException
1784 * If the FTP server prematurely closes the connection as a result
1785 * of the client being idle or some other reason causing the server
1786 * to send FTP reply code 421. This exception may be caught either
1787 * as an IOException or independently as itself.
1788 * @exception IOException If an I/O error occurs while either sending a
1789 * command to the server or receiving a reply from the server.
1790 ***/
1791 public String listHelp(String command) throws IOException
1792 {
1793 if (FTPReply.isPositiveCompletion(help(command)))
1794 return getReplyString();
1795 return null;
1796 }
1797
1798
1799 /****
1800 * Sends a NOOP command to the FTP server. This is useful for preventing
1801 * server timeouts.
1802 * <p>
1803 * @return True if successfully completed, false if not.
1804 * @exception FTPConnectionClosedException
1805 * If the FTP server prematurely closes the connection as a result
1806 * of the client being idle or some other reason causing the server
1807 * to send FTP reply code 421. This exception may be caught either
1808 * as an IOException or independently as itself.
1809 * @exception IOException If an I/O error occurs while either sending a
1810 * command to the server or receiving a reply from the server.
1811 ***/
1812 public boolean sendNoOp() throws IOException
1813 {
1814 return FTPReply.isPositiveCompletion(noop());
1815 }
1816
1817
1818 /****
1819 * Obtain a list of filenames in a directory (or just the name of a given
1820 * file, which is not particularly useful). This information is obtained
1821 * through the NLST command. If the given pathname is a directory and
1822 * contains no files, a zero length array is returned only
1823 * if the FTP server returned a positive completion code, otherwise
1824 * null is returned (the FTP server returned a 550 error No files found.).
1825 * If the directory is not empty, an array of filenames in the directory is
1826 * returned. If the pathname corresponds
1827 * to a file, only that file will be listed. The server may or may not
1828 * expand glob expressions.
1829 * <p>
1830 * @param pathname The file or directory to list.
1831 * @return The list of filenames contained in the given path. null if
1832 * the list could not be obtained. If there are no filenames in
1833 * the directory, a zero-length array is returned.
1834 * @exception FTPConnectionClosedException
1835 * If the FTP server prematurely closes the connection as a result
1836 * of the client being idle or some other reason causing the server
1837 * to send FTP reply code 421. This exception may be caught either
1838 * as an IOException or independently as itself.
1839 * @exception IOException If an I/O error occurs while either sending a
1840 * command to the server or receiving a reply from the server.
1841 ***/
1842 public String[] listNames(String pathname) throws IOException
1843 {
1844 String line;
1845 Socket socket;
1846 BufferedReader reader;
1847 Vector results;
1848
1849 if ((socket = __openDataConnection(FTPCommand.NLST, pathname)) == null)
1850 return null;
1851
1852 reader =
1853 new BufferedReader(new InputStreamReader(socket.getInputStream()));
1854
1855 results = new Vector();
1856 while ((line = reader.readLine()) != null)
1857 results.addElement(line);
1858 reader.close();
1859 socket.close();
1860
1861 if (completePendingCommand())
1862 {
1863 String[] result;
1864 result = new String[results.size()];
1865 results.copyInto(result);
1866 return result;
1867 }
1868
1869 return null;
1870 }
1871
1872
1873 /****
1874 * Obtain a list of filenames in the current working directory
1875 * This information is obtained through the NLST command. If the current
1876 * directory contains no files, a zero length array is returned only
1877 * if the FTP server returned a positive completion code, otherwise,
1878 * null is returned (the FTP server returned a 550 error No files found.).
1879 * If the directory is not empty, an array of filenames in the directory is
1880 * returned.
1881 * <p>
1882 * @return The list of filenames contained in the current working
1883 * directory. null if the list could not be obtained.
1884 * If there are no filenames in the directory, a zero-length array
1885 * is returned.
1886 * @exception FTPConnectionClosedException
1887 * If the FTP server prematurely closes the connection as a result
1888 * of the client being idle or some other reason causing the server
1889 * to send FTP reply code 421. This exception may be caught either
1890 * as an IOException or independently as itself.
1891 * @exception IOException If an I/O error occurs while either sending a
1892 * command to the server or receiving a reply from the server.
1893 ***/
1894 public String[] listNames() throws IOException
1895 {
1896 return listNames(null);
1897 }
1898
1899
1900
1901 /****
1902 * Using a programmer specified <code> FTPFileListParser </code>, obtain a
1903 * list of file information for a directory or information for
1904 * just a single file. This information is obtained through the LIST
1905 * command. The contents of the returned array is determined by the
1906 * <code> FTPFileListParser </code> used.
1907 * The server may or may not expand glob expressions. You should avoid
1908 * using glob expressions because the return format for glob listings
1909 * differs from server to server and will likely cause this method to fail.
1910 * <p>
1911 * @param parser The <code> FTPFileListParser </code> that should be
1912 * used to parse the server file listing.
1913 * @param pathname The file or directory to list.
1914 * @return The list of file information contained in the given path in
1915 * the format determined by the <code> parser </code> parameter.
1916 * @exception FTPConnectionClosedException
1917 * If the FTP server prematurely closes the connection as a result
1918 * of the client being idle or some other reason causing the server
1919 * to send FTP reply code 421. This exception may be caught either
1920 * as an IOException or independently as itself.
1921 * @exception IOException If an I/O error occurs while either sending a
1922 * command to the server or receiving a reply from the server.
1923 ***/
1924 public FTPFile[] listFiles(FTPFileListParser parser, String pathname)
1925 throws IOException
1926 {
1927 Socket socket;
1928 FTPFile[] results;
1929
1930 if ((socket = __openDataConnection(FTPCommand.LIST, pathname)) == null)
1931 return null;
1932
1933 results = parser.parseFileList(socket.getInputStream());
1934
1935 socket.close();
1936
1937 completePendingCommand();
1938
1939 return results;
1940 }
1941
1942
1943 /****
1944 * Using a programmer specified <code> FTPFileListParser </code>,
1945 * obtain a list of file information for the current working directory.
1946 * This information is obtained through the LIST command.
1947 * The contents of the array returned is determined by the
1948 * <code> FTPFileListParser </code> used.
1949 * <p>
1950 * @param parser The <code> FTPFileListParser </code> that should be
1951 * used to parse the server file listing.
1952 * @return The list of file information contained in the given path in
1953 * the format determined by the <code> parser </code> parameter.
1954 * @exception FTPConnectionClosedException
1955 * If the FTP server prematurely closes the connection as a result
1956 * of the client being idle or some other reason causing the server
1957 * to send FTP reply code 421. This exception may be caught either
1958 * as an IOException or independently as itself.
1959 * @exception IOException If an I/O error occurs while either sending a
1960 * command to the server or receiving a reply from the server.
1961 ***/
1962 public FTPFile[] listFiles(FTPFileListParser parser) throws IOException
1963 {
1964 return listFiles(parser, null);
1965 }
1966
1967
1968 /****
1969 * Using the <code> DefaultFTPFileListParser </code>, obtain a list of
1970 * file information
1971 * for a directory or information for just a single file. This information
1972 * is obtained through the LIST command. If the given
1973 * pathname is a directory and contains no files, <code> null </code> is
1974 * returned, otherwise an array of <code> FTPFile </code> instances
1975 * representing the files in the directory is returned.
1976 * If the pathname corresponds to a file, only the information for that
1977 * file will be contained in the array (which will be of length 1). The
1978 * server may or may not expand glob expressions. You should avoid using
1979 * glob expressions because the return format for glob listings differs
1980 * from server to server and will likely cause this method to fail.
1981 * <p>
1982 * @param pathname The file or directory to list.
1983 * @return The list of file information contained in the given path. null
1984 * if the list could not be obtained or if there are no files in
1985 * the directory.
1986 * @exception FTPConnectionClosedException
1987 * If the FTP server prematurely closes the connection as a result
1988 * of the client being idle or some other reason causing the server
1989 * to send FTP reply code 421. This exception may be caught either
1990 * as an IOException or independently as itself.
1991 * @exception IOException If an I/O error occurs while either sending a
1992 * command to the server or receiving a reply from the server.
1993 ***/
1994 public FTPFile[] listFiles(String pathname) throws IOException
1995 {
1996 return listFiles(__fileListParser, pathname);
1997 }
1998
1999 /****
2000 * Using the <code> DefaultFTPFileListParser </code>, obtain a list of
2001 * file information for the current working directory. This information
2002 * is obtained through the LIST command. If the given
2003 * current directory contains no files null is returned, otherwise an
2004 * array of <code> FTPFile </code> instances representing the files in the
2005 * directory is returned.
2006 * <p>
2007 * @return The list of file information contained in the current working
2008 * directory. null if the list could not be obtained or if there are
2009 * no files in the directory.
2010 * @exception FTPConnectionClosedException
2011 * If the FTP server prematurely closes the connection as a result
2012 * of the client being idle or some other reason causing the server
2013 * to send FTP reply code 421. This exception may be caught either
2014 * as an IOException or independently as itself.
2015 * @exception IOException If an I/O error occurs while either sending a
2016 * command to the server or receiving a reply from the server.
2017 ***/
2018 public FTPFile[] listFiles() throws IOException
2019 {
2020 return listFiles(__fileListParser);
2021 }
2022
2023
2024 /****
2025 * Issue the FTP STAT command to the server.
2026 * <p>
2027 * @return The status information returned by the server.
2028 * @exception FTPConnectionClosedException
2029 * If the FTP server prematurely closes the connection as a result
2030 * of the client being idle or some other reason causing the server
2031 * to send FTP reply code 421. This exception may be caught either
2032 * as an IOException or independently as itself.
2033 * @exception IOException If an I/O error occurs while either sending a
2034 * command to the server or receiving a reply from the server.
2035 ***/
2036 public String getStatus() throws IOException
2037 {
2038 if (FTPReply.isPositiveCompletion(stat()))
2039 return getReplyString();
2040 return null;
2041 }
2042
2043
2044 /****
2045 * Issue the FTP STAT command to the server for a given pathname. This
2046 * should produce a listing of the file or directory.
2047 * <p>
2048 * @return The status information returned by the server.
2049 * @exception FTPConnectionClosedException
2050 * If the FTP server prematurely closes the connection as a result
2051 * of the client being idle or some other reason causing the server
2052 * to send FTP reply code 421. This exception may be caught either
2053 * as an IOException or independently as itself.
2054 * @exception IOException If an I/O error occurs while either sending a
2055 * command to the server or receiving a reply from the server.
2056 ***/
2057 public String getStatus(String pathname) throws IOException
2058 {
2059 if (FTPReply.isPositiveCompletion(stat(pathname)))
2060 return getReplyString();
2061 return null;
2062 }
2063
2064
2065 }
This page was automatically generated by Maven