Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.commons.net.SocketClient
org.apache.commons.net.telnet.Telnet
org.apache.commons.net.telnet.TelnetClient
org.apache.commons.net.ftp.FTP
public class FTP
extends TelnetClient
FTPClient
class,
derived from FTP,
implements all the functionality required of an FTP client. The
FTP class is made public to provide access to various FTP constants
and to make it easier for adventurous programmers (or those with
special needs) to interact with the FTP protocol and implement their
own clients. A set of methods with names corresponding to the FTP
command names are provided to facilitate this interaction.
You should keep in mind that the FTP server may choose to prematurely
close a connection if the client has been idle for longer than a
given time period (usually 900 seconds). The FTP class will detect a
premature FTP server connection closing when it receives a
FTPReply.SERVICE_NOT_AVAILABLE
response to a command.
When that occurs, the FTP class method encountering that reply will throw
an FTPConnectionClosedException
. FTPConectionClosedException
is a subclass of IOException
and therefore need not be
caught separately, but if you are going to catch it separately, its
catch block must appear before the more general IOException
catch block. When you encounter an
FTPConnectionClosedException
, you must disconnect the connection with
disconnect()
to properly clean up the
system resources used by FTP. Before disconnecting, you may check the
last reply code and text with
getReplyCode
,
getReplyString
,
and getReplyStrings
.
You may avoid server disconnections while the client is idle by
periodicaly sending NOOP commands to the server.
Rather than list it separately for each method, we mention here that
every method communicating with the server and throwing an IOException
can also throw a
MalformedServerReplyException
, which is a subclass
of IOException. A MalformedServerReplyException will be thrown when
the reply received from the server deviates enough from the protocol
specification that it cannot be interpreted in a useful manner despite
attempts to be as lenient as possible.
Field Summary | |
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static String |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
static int |
|
protected ProtocolCommandSupport |
|
Fields inherited from class org.apache.commons.net.telnet.TelnetClient | |
readerThread |
Fields inherited from class org.apache.commons.net.telnet.Telnet | |
TERMINAL_TYPE , TERMINAL_TYPE_IS , TERMINAL_TYPE_SEND |
Fields inherited from class org.apache.commons.net.SocketClient | |
NETASCII_EOL , _defaultPort_ , _input_ , _isConnected_ , _output_ , _socketFactory_ , _socket_ , _timeout_ |
Constructor Summary | |
|
Method Summary | |
protected void |
|
int |
|
int |
|
void |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
void |
|
String | |
int |
|
int |
|
String |
|
String[] |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
void |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
void |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
int |
|
Methods inherited from class org.apache.commons.net.telnet.TelnetClient | |
_connectAction_ , addOptionHandler , deleteOptionHandler , disconnect , getInputStream , getLocalOptionState , getOutputStream , getReaderThread , getRemoteOptionState , registerNotifHandler , registerSpyStream , sendAYT , setReaderThread , stopSpyStream , unregisterNotifHandler |
Methods inherited from class org.apache.commons.net.telnet.Telnet | |
_connectAction_ , registerNotifHandler , unregisterNotifHandler |
Methods inherited from class org.apache.commons.net.SocketClient | |
_connectAction_ , connect , connect , connect , connect , connect , connect , disconnect , getDefaultPort , getDefaultTimeout , getLocalAddress , getLocalPort , getRemoteAddress , getRemotePort , getSoLinger , getSoTimeout , getTcpNoDelay , isConnected , setDefaultPort , setDefaultTimeout , setSoLinger , setSoTimeout , setSocketFactory , setTcpNoDelay , verifyRemote |
public static final int ASCII_FILE_TYPE
A constant used to indicate the file(s) being transfered should be treated as ASCII. This is the default file type. All constants ending inFILE_TYPE
are used to indicate file types.
- Field Value:
- 0
public static final int BINARY_FILE_TYPE
A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed. All constants ending inFILE_TYPE
are used to indicate file types.
- Field Value:
- 2
public static final int BLOCK_TRANSFER_MODE
A constant used to indicate a file is to be transfered as a series of blocks. All constants ending inTRANSFER_MODE
are used to indicate file transfer modes.
- Field Value:
- 11
public static final int CARRIAGE_CONTROL_TEXT_FORMAT
A constant used to indicate a text file contains ASA vertical format control characters. All constants ending inTEXT_FORMAT
are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
- Field Value:
- 6
public static final int COMPRESSED_TRANSFER_MODE
A constant used to indicate a file is to be transfered as FTP compressed data. All constants ending inTRANSFER_MODE
are used to indicate file transfer modes.
- Field Value:
- 12
public static final String DEFAULT_CONTROL_ENCODING
The default character encoding used for communicating over an FTP control connection. The default encoding is an ASCII-compatible encoding. Some FTP servers expect other encodings. You can change the encoding used by an FTP instance withsetControlEncoding
.
public static final int DEFAULT_DATA_PORT
The default FTP data port (20). **
- Field Value:
- 20
public static final int DEFAULT_PORT
The default FTP control port (21). **
- Field Value:
- 21
public static final int EBCDIC_FILE_TYPE
A constant used to indicate the file(s) being transfered should be treated as EBCDIC. Note however that there are several different EBCDIC formats. All constants ending inFILE_TYPE
are used to indicate file types.
- Field Value:
- 1
public static final int FILE_STRUCTURE
A constant used to indicate a file is to be treated as a continuous sequence of bytes. This is the default structure. All constants ending in_STRUCTURE
are used to indicate file structure for file transfers.
- Field Value:
- 7
public static final int IMAGE_FILE_TYPE
A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed. All constants ending inFILE_TYPE
are used to indicate file types.
- Field Value:
- 2
public static final int LOCAL_FILE_TYPE
A constant used to indicate the file(s) being transfered should be treated as a local type. All constants ending inFILE_TYPE
are used to indicate file types.
- Field Value:
- 3
public static final int NON_PRINT_TEXT_FORMAT
A constant used for text files to indicate a non-print text format. This is the default format. All constants ending inTEXT_FORMAT
are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
- Field Value:
- 4
public static final int PAGE_STRUCTURE
A constant used to indicate a file is to be treated as a set of independent indexed pages. All constants ending in_STRUCTURE
are used to indicate file structure for file transfers.
- Field Value:
- 9
public static final int RECORD_STRUCTURE
A constant used to indicate a file is to be treated as a sequence of records. All constants ending in_STRUCTURE
are used to indicate file structure for file transfers.
- Field Value:
- 8
public static final int STREAM_TRANSFER_MODE
A constant used to indicate a file is to be transfered as a stream of bytes. This is the default transfer mode. All constants ending inTRANSFER_MODE
are used to indicate file transfer modes.
- Field Value:
- 10
public static final int TELNET_TEXT_FORMAT
A constant used to indicate a text file contains format vertical format control characters. All constants ending inTEXT_FORMAT
are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
- Field Value:
- 5
protected ProtocolCommandSupport _commandSupport_
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
public FTP()
The default FTP constructor. Sets the default port toDEFAULT_PORT
and initializes internal data structures for saving FTP reply information.
protected void _connectAction_() throws IOException
Handles special connection requirements.
- Overrides:
- _connectAction_ in interface TelnetClient
public int abor() throws IOException
A convenience method to send the FTP ABOR command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int acct(String account) throws IOException
A convenience method to send the FTP ACCT command to the server, receive the reply, and return the reply code.
- Parameters:
account
- The account name to access.
- Returns:
- The reply code received from the server.
public void addProtocolCommandListener(ProtocolCommandListener listener)
Adds a ProtocolCommandListener. Delegates this task to_commandSupport_
.
- Parameters:
listener
- The ProtocolCommandListener to add.
public int allo(int bytes) throws IOException
A convenience method to send the FTP ALLO command to the server, receive the reply, and return the reply code.
- Parameters:
bytes
- The number of bytes to allocate.
- Returns:
- The reply code received from the server.
public int allo(int bytes, int recordSize) throws IOException
A convenience method to send the FTP ALLO command to the server, receive the reply, and return the reply code.
- Parameters:
bytes
- The number of bytes to allocate.recordSize
- The size of a record.
- Returns:
- The reply code received from the server.
public int appe(String pathname) throws IOException
A convenience method to send the FTP APPE command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The pathname to use for the file when stored at the remote end of the transfer.
- Returns:
- The reply code received from the server.
public int cdup() throws IOException
A convenience method to send the FTP CDUP command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int cwd(String directory) throws IOException
A convenience method to send the FTP CWD command to the server, receive the reply, and return the reply code.
- Parameters:
directory
- The new working directory.
- Returns:
- The reply code received from the server.
public int dele(String pathname) throws IOException
A convenience method to send the FTP DELE command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- The pathname to delete.
- Returns:
- The reply code received from the server.
public void disconnect() throws IOException
Closes the control connection to the FTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector. The reply text and code information from the last command is voided so that the memory it used may be reclaimed.
- Overrides:
- disconnect in interface TelnetClient
public String getControlEncoding()
- Returns:
- The character encoding used to communicate over the control connection.
public int getReply() throws IOException
Fetches a reply from the FTP server and returns the integer reply code. After calling this method, the actual reply text can be accessed from either callinggetReplyString
orgetReplyStrings
. Only use this method if you are implementing your own FTP client or if you need to fetch a secondary response from the FTP server.
- Returns:
- The integer value of the reply code of the fetched FTP reply.
public int getReplyCode()
Returns the integer value of the reply code of the last FTP reply. You will usually only use this method after you connect to the FTP server to check that the connection was successful sinceconnect
is of type void.
- Returns:
- The integer value of the reply code of the last FTP reply.
public String getReplyString()
Returns the entire text of the last FTP server response exactly as it was received, including all end of line markers in NETASCII format.
- Returns:
- The entire text from the last FTP response as a String.
public String[] getReplyStrings()
Returns the lines of text from the last FTP server response as an array of strings, one entry per line. The end of line markers of each are stripped from each line.
- Returns:
- The lines of text from the last FTP response as an array.
public int help() throws IOException
A convenience method to send the FTP HELP command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int help(String command) throws IOException
A convenience method to send the FTP HELP command to the server, receive the reply, and return the reply code.
- Parameters:
command
- The command name on which to request help.
- Returns:
- The reply code received from the server.
public int list() throws IOException
A convenience method to send the FTP LIST command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Returns:
- The reply code received from the server.
public int list(String pathname) throws IOException
A convenience method to send the FTP LIST command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The pathname to list.
- Returns:
- The reply code received from the server.
public int mkd(String pathname) throws IOException
A convenience method to send the FTP MKD command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- The pathname of the new directory to create.
- Returns:
- The reply code received from the server.
public int mode(int mode) throws IOException
A convenience method to send the FTP MODE command to the server, receive the reply, and return the reply code.
- Parameters:
mode
- The transfer mode to use (one of theTRANSFER_MODE
constants).
- Returns:
- The reply code received from the server.
public int nlst() throws IOException
A convenience method to send the FTP NLST command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Returns:
- The reply code received from the server.
public int nlst(String pathname) throws IOException
A convenience method to send the FTP NLST command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The pathname to list.
- Returns:
- The reply code received from the server.
public int noop() throws IOException
A convenience method to send the FTP NOOP command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int pass(String password) throws IOException
A convenience method to send the FTP PASS command to the server, receive the reply, and return the reply code.
- Parameters:
password
- The plain text password of the username being logged into.
- Returns:
- The reply code received from the server.
public int pasv() throws IOException
A convenience method to send the FTP PASV command to the server, receive the reply, and return the reply code. Remember, it's up to you to interpret the reply string containing the host/port information.
- Returns:
- The reply code received from the server.
public int port(InetAddress host, int port) throws IOException
A convenience method to send the FTP PORT command to the server, receive the reply, and return the reply code.
- Parameters:
host
- The host owning the port.port
- The new port.
- Returns:
- The reply code received from the server.
public int pwd() throws IOException
A convenience method to send the FTP PWD command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int quit() throws IOException
A convenience method to send the FTP QUIT command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int rein() throws IOException
A convenience method to send the FTP REIN command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public void removeProtocolCommandListener(ProtocolCommandListener listener)
Removes a ProtocolCommandListener. Delegates this task to_commandSupport_
.
- Parameters:
listener
- The ProtocolCommandListener to remove.
public int rest(String marker) throws IOException
A convenience method to send the FTP REST command to the server, receive the reply, and return the reply code.
- Parameters:
marker
- The marker at which to restart a transfer.
- Returns:
- The reply code received from the server.
public int retr(String pathname) throws IOException
A convenience method to send the FTP RETR command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The pathname of the file to retrieve.
- Returns:
- The reply code received from the server.
public int rmd(String pathname) throws IOException
A convenience method to send the FTP RMD command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- The pathname of the directory to remove.
- Returns:
- The reply code received from the server.
public int rnfr(String pathname) throws IOException
A convenience method to send the FTP RNFR command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- The pathname to rename from.
- Returns:
- The reply code received from the server.
public int rnto(String pathname) throws IOException
A convenience method to send the FTP RNTO command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- The pathname to rename to
- Returns:
- The reply code received from the server.
public int sendCommand(String command) throws IOException
Sends an FTP command with no arguments to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyString
orgetReplyStrings
.
- Parameters:
command
- The text representation of the FTP command to send.
- Returns:
- The integer value of the FTP reply code returned by the server in response to the command.
public int sendCommand(String command, String args) throws IOException
Sends an FTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyString
orgetReplyStrings
.
- Parameters:
command
- The text representation of the FTP command to send.args
- The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
- Returns:
- The integer value of the FTP reply code returned by the server in response to the command.
public int sendCommand(int command) throws IOException
Sends an FTP command with no arguments to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyString
orgetReplyStrings
.
- Parameters:
command
- The FTPCommand constant corresponding to the FTP command to send.
- Returns:
- The integer value of the FTP reply code returned by the server in response to the command.
public int sendCommand(int command, String args) throws IOException
Sends an FTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyString
orgetReplyStrings
.
- Parameters:
command
- The FTPCommand constant corresponding to the FTP command to send.args
- The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
- Returns:
- The integer value of the FTP reply code returned by the server in response to the command.
public void setControlEncoding(String encoding)
Sets the character encoding used by the FTP control connection. Some FTP servers require that commands be issued in a non-ASCII encoding like UTF-8 so that filenames with multi-byte character representations (e.g, Big 8) can be specified.
- Parameters:
encoding
- The new character encoding for the control connection.
public int site(String parameters) throws IOException
A convenience method to send the FTP SITE command to the server, receive the reply, and return the reply code.
- Parameters:
parameters
- The site parameters to send.
- Returns:
- The reply code received from the server.
public int smnt(String dir) throws IOException
A convenience method to send the FTP SMNT command to the server, receive the reply, and return the reply code.
- Parameters:
dir
- The directory name.
- Returns:
- The reply code received from the server.
public int stat() throws IOException
A convenience method to send the FTP STAT command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int stat(String pathname) throws IOException
A convenience method to send the FTP STAT command to the server, receive the reply, and return the reply code.
- Parameters:
pathname
- A pathname to list.
- Returns:
- The reply code received from the server.
public int stor(String pathname) throws IOException
A convenience method to send the FTP STOR command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The pathname to use for the file when stored at the remote end of the transfer.
- Returns:
- The reply code received from the server.
public int stou() throws IOException
A convenience method to send the FTP STOU command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Returns:
- The reply code received from the server.
public int stou(String pathname) throws IOException
A convenience method to send the FTP STOU command to the server, receive the reply, and return the reply code. Remember, it is up to you to manage the data connection. If you don't need this low level of access, useFTPClient
, which will handle all low level details for you.
- Parameters:
pathname
- The base pathname to use for the file when stored at the remote end of the transfer. Some FTP servers require this.
- Returns:
- The reply code received from the server.
public int stru(int structure) throws IOException
A convenience method to send the FTP STRU command to the server, receive the reply, and return the reply code.
- Parameters:
structure
- The structure of the file (one of the_STRUCTURE
constants).
- Returns:
- The reply code received from the server.
public int syst() throws IOException
A convenience method to send the FTP SYST command to the server, receive the reply, and return the reply code.
- Returns:
- The reply code received from the server.
public int type(int fileType) throws IOException
A convenience method to send the FTP TYPE command to the server, receive the reply, and return the reply code.
- Parameters:
fileType
- The type of the file (one of theFILE_TYPE
constants).
- Returns:
- The reply code received from the server.
public int type(int fileType, int formatOrByteSize) throws IOException
A convenience method to send the FTP TYPE command for text files to the server, receive the reply, and return the reply code.
- Parameters:
fileType
- The type of the file (one of theFILE_TYPE
constants).formatOrByteSize
- The format of the file (one of the_FORMAT
constants. In the case ofLOCAL_FILE_TYPE
, the byte size.
- Returns:
- The reply code received from the server.
public int user(String username) throws IOException
A convenience method to send the FTP USER command to the server, receive the reply, and return the reply code.
- Parameters:
username
- The username to login under.
- Returns:
- The reply code received from the server.