FileDocCategorySizeDatePackage
FTP.javaAPI DocApache Commons NET 1.4.1 API62216Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.ftp

FTP

public class FTP extends org.apache.commons.net.telnet.TelnetClient
FTP provides the basic the functionality necessary to implement your own FTP client. It extends org.apache.commons.net.TelnetClient simply because it saves the writing of extra code to handle the FTP control connection which always remains open during an FTP session and uses the Telnet protocol. Aggregation would require writing new wrapper methods and wouldn't leverage the functionality already present in org.apache.commons.net.SocketClient.

To derive the full benefits of the FTP class requires some knowledge of the FTP protocol defined in RFC 959. However, there is no reason why you should have to use the FTP class. The {@link org.apache.commons.net.ftp.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 {@link org.apache.commons.net.ftp.FTPReply#SERVICE_NOT_AVAILABLE FTPReply.SERVICE_NOT_AVAILABLE } response to a command. When that occurs, the FTP class method encountering that reply will throw an {@link org.apache.commons.net.ftp.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 {@link org.apache.commons.net.ftp.FTPConnectionClosedException} , you must disconnect the connection with {@link #disconnect disconnect() } to properly clean up the system resources used by FTP. Before disconnecting, you may check the last reply code and text with {@link #getReplyCode getReplyCode }, {@link #getReplyString getReplyString }, and {@link #getReplyStrings 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 {@link org.apache.commons.net.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.

author
Daniel F. Savarese
see
FTPClient
see
FTPConnectionClosedException
see
org.apache.commons.net.MalformedServerReplyException

Fields Summary
public static final int
DEFAULT_DATA_PORT
The default FTP data port (20).
public static final int
DEFAULT_PORT
The default FTP control port (21).
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 in FILE_TYPE are used to indicate file types.
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 in FILE_TYPE are used to indicate file types.
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 in FILE_TYPE are used to indicate file types.
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 in FILE_TYPE are used to indicate file types.
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 in FILE_TYPE are used to indicate file types.
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 in TEXT_FORMAT are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
public static final int
TELNET_TEXT_FORMAT
A constant used to indicate a text file contains format vertical format control characters. All constants ending in TEXT_FORMAT are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
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 in TEXT_FORMAT are used to indicate text formatting for text transfers (both ASCII and EBCDIC).
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.
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.
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.
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 in TRANSFER_MODE are used to indicate file transfer modes.
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 in TRANSFER_MODE are used to indicate file transfer modes.
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 in TRANSFER_MODE are used to indicate file transfer modes.
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 with {@link #setControlEncoding setControlEncoding}.
private static final String
__modes
private StringBuffer
__commandBuffer
BufferedReader
_controlInput
BufferedWriter
_controlOutput
int
_replyCode
Vector
_replyLines
boolean
_newReplyString
String
_replyString
String
_controlEncoding
protected org.apache.commons.net.ProtocolCommandSupport
_commandSupport_
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
Constructors Summary
public FTP()
The default FTP constructor. Sets the default port to DEFAULT_PORT and initializes internal data structures for saving FTP reply information.


                              
     
    
        setDefaultPort(DEFAULT_PORT);
        __commandBuffer = new StringBuffer();
        _replyLines = new Vector();
        _newReplyString = false;
        _replyString = null;
        _commandSupport_ = new ProtocolCommandSupport(this);
        _controlEncoding = DEFAULT_CONTROL_ENCODING;
    
Methods Summary
private void__getReply()

        int length;

        _newReplyString = true;
        _replyLines.setSize(0);

        String line = _controlInput.readLine();

        if (line == null)
            throw new FTPConnectionClosedException(
                "Connection closed without indication.");

        // In case we run into an anomaly we don't want fatal index exceptions
        // to be thrown.
        length = line.length();
        if (length < 3)
            throw new MalformedServerReplyException(
                "Truncated server reply: " + line);

        try
        {
            String code = line.substring(0, 3);
            _replyCode = Integer.parseInt(code);
        }
        catch (NumberFormatException e)
        {
            throw new MalformedServerReplyException(
                "Could not parse response code.\nServer Reply: " + line);
        }

        _replyLines.addElement(line);

        // Get extra lines if message continues.
        if (length > 3 && line.charAt(3) == '-")
        {
            do
            {
                line = _controlInput.readLine();

                if (line == null)
                    throw new FTPConnectionClosedException(
                        "Connection closed without indication.");

                _replyLines.addElement(line);

                // The length() check handles problems that could arise from readLine()
                // returning too soon after encountering a naked CR or some other
                // anomaly.
            }
            while (!(line.length() >= 4 && line.charAt(3) != '-" &&
                     Character.isDigit(line.charAt(0))));
            // This is too strong a condition because of non-conforming ftp
            // servers like ftp.funet.fi which sent 226 as the last line of a
            // 426 multi-line reply in response to ls /.  We relax the condition to
            // test that the line starts with a digit rather than starting with
            // the code.
            // line.startsWith(code)));
        }

        if (_commandSupport_.getListenerCount() > 0)
            _commandSupport_.fireReplyReceived(_replyCode, getReplyString());

        if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE)
            throw new FTPConnectionClosedException(
                "FTP response 421 received.  Server closed connection.");
    
protected void_connectAction_()

        super._connectAction_();
        _controlInput =
            new BufferedReader(new InputStreamReader(getInputStream(),
                                                     getControlEncoding()));
        _controlOutput =
            new BufferedWriter(new OutputStreamWriter(getOutputStream(),
                                                      getControlEncoding()));
        __getReply();
        // If we received code 120, we have to fetch completion reply.
        if (FTPReply.isPositivePreliminary(_replyCode))
            __getReply();
    
public intabor()
A convenience method to send the FTP ABOR command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.ABOR);
    
public intacct(java.lang.String account)
A convenience method to send the FTP ACCT command to the server, receive the reply, and return the reply code.

param
account The account name to access.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.ACCT, account);
    
public voidaddProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener listener)
Adds a ProtocolCommandListener. Delegates this task to {@link #_commandSupport_ _commandSupport_ }.

param
listener The ProtocolCommandListener to add.

        _commandSupport_.addProtocolCommandListener(listener);
    
public intallo(int bytes)
A convenience method to send the FTP ALLO command to the server, receive the reply, and return the reply code.

param
bytes The number of bytes to allocate.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.ALLO, Integer.toString(bytes));
    
public intallo(int bytes, int recordSize)
A convenience method to send the FTP ALLO command to the server, receive the reply, and return the reply code.

param
bytes The number of bytes to allocate.
param
recordSize The size of a record.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.ALLO, Integer.toString(bytes) + " R " +
                           Integer.toString(recordSize));
    
public intappe(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The pathname to use for the file when stored at the remote end of the transfer.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.APPE, pathname);
    
public intcdup()
A convenience method to send the FTP CDUP command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.CDUP);
    
public intcwd(java.lang.String directory)
A convenience method to send the FTP CWD command to the server, receive the reply, and return the reply code.

param
directory The new working directory.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.CWD, directory);
    
public intdele(java.lang.String pathname)
A convenience method to send the FTP DELE command to the server, receive the reply, and return the reply code.

param
pathname The pathname to delete.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.DELE, pathname);
    
public voiddisconnect()
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.

exception
IOException If an error occurs while disconnecting.

        super.disconnect();
        _controlInput = null;
        _controlOutput = null;
        _replyLines.setSize(0);
        _newReplyString = false;
        _replyString = null;
    
public java.lang.StringgetControlEncoding()

return
The character encoding used to communicate over the control connection.

        return _controlEncoding;
    
public intgetReply()
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 calling {@link #getReplyString getReplyString } or {@link #getReplyStrings getReplyStrings }. 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.

return
The integer value of the reply code of the fetched FTP reply.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while receiving the server reply.

        __getReply();
        return _replyCode;
    
public intgetReplyCode()
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 since connect is of type void.

return
The integer value of the reply code of the last FTP reply.

        return _replyCode;
    
public java.lang.StringgetReplyString()
Returns the entire text of the last FTP server response exactly as it was received, including all end of line markers in NETASCII format.

return
The entire text from the last FTP response as a String.

        Enumeration en;
        StringBuffer buffer;

        if (!_newReplyString)
            return _replyString;

        buffer = new StringBuffer(256);
        en = _replyLines.elements();
        while (en.hasMoreElements())
        {
            buffer.append((String)en.nextElement());
            buffer.append(SocketClient.NETASCII_EOL);
        }

        _newReplyString = false;

        return (_replyString = buffer.toString());
    
public java.lang.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.

return
The lines of text from the last FTP response as an array.

        String[] lines;
        lines = new String[_replyLines.size()];
        _replyLines.copyInto(lines);
        return lines;
    
public inthelp()
A convenience method to send the FTP HELP command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.HELP);
    
public inthelp(java.lang.String command)
A convenience method to send the FTP HELP command to the server, receive the reply, and return the reply code.

param
command The command name on which to request help.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.HELP, command);
    
public intlist()
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.LIST);
    
public intlist(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The pathname to list.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.LIST, pathname);
    
public intmkd(java.lang.String pathname)
A convenience method to send the FTP MKD command to the server, receive the reply, and return the reply code.

param
pathname The pathname of the new directory to create.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.MKD, pathname);
    
public intmode(int mode)
A convenience method to send the FTP MODE command to the server, receive the reply, and return the reply code.

param
mode The transfer mode to use (one of the TRANSFER_MODE constants).
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.MODE,
                           __modes.substring(mode, mode + 1));
    
public intnlst()
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.NLST);
    
public intnlst(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The pathname to list.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.NLST, pathname);
    
public intnoop()
A convenience method to send the FTP NOOP command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.NOOP);
    
public intpass(java.lang.String password)
A convenience method to send the FTP PASS command to the server, receive the reply, and return the reply code.

param
password The plain text password of the username being logged into.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.PASS, password);
    
public intpasv()
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.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.PASV);
    
public intport(java.net.InetAddress host, int port)
A convenience method to send the FTP PORT command to the server, receive the reply, and return the reply code.

param
host The host owning the port.
param
port The new port.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        int num;
        StringBuffer info = new StringBuffer(24);

        info.append(host.getHostAddress().replace('.", ',"));
        num = port >>> 8;
        info.append(',");
        info.append(num);
        info.append(',");
        num = port & 0xff;
        info.append(num);

        return sendCommand(FTPCommand.PORT, info.toString());
    
public intpwd()
A convenience method to send the FTP PWD command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.PWD);
    
public intquit()
A convenience method to send the FTP QUIT command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.QUIT);
    
public intrein()
A convenience method to send the FTP REIN command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.REIN);
    
public voidremoveProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener listener)
Removes a ProtocolCommandListener. Delegates this task to {@link #_commandSupport_ _commandSupport_ }.

param
listener The ProtocolCommandListener to remove.

        _commandSupport_.removeProtocolCommandListener(listener);
    
public intrest(java.lang.String marker)
A convenience method to send the FTP REST command to the server, receive the reply, and return the reply code.

param
marker The marker at which to restart a transfer.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.REST, marker);
    
public intretr(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The pathname of the file to retrieve.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.RETR, pathname);
    
public intrmd(java.lang.String pathname)
A convenience method to send the FTP RMD command to the server, receive the reply, and return the reply code.

param
pathname The pathname of the directory to remove.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.RMD, pathname);
    
public intrnfr(java.lang.String pathname)
A convenience method to send the FTP RNFR command to the server, receive the reply, and return the reply code.

param
pathname The pathname to rename from.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.RNFR, pathname);
    
public intrnto(java.lang.String pathname)
A convenience method to send the FTP RNTO command to the server, receive the reply, and return the reply code.

param
pathname The pathname to rename to
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.RNTO, pathname);
    
public intsendCommand(int command, java.lang.String args)
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 calling {@link #getReplyString getReplyString } or {@link #getReplyStrings getReplyStrings }.

param
command The FTPCommand constant corresponding to the FTP command to send.
param
args The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
return
The integer value of the FTP reply code returned by the server in response to the command.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand._commands[command], args);
    
public intsendCommand(java.lang.String command)
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 calling {@link #getReplyString getReplyString } or {@link #getReplyStrings getReplyStrings }.

param
command The text representation of the FTP command to send.
return
The integer value of the FTP reply code returned by the server in response to the command.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(command, null);
    
public intsendCommand(int command)
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 calling {@link #getReplyString getReplyString } or {@link #getReplyStrings getReplyStrings }.

param
command The FTPCommand constant corresponding to the FTP command to send.
return
The integer value of the FTP reply code returned by the server in response to the command.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(command, null);
    
public intsendCommand(java.lang.String command, java.lang.String args)
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 calling {@link #getReplyString getReplyString } or {@link #getReplyStrings getReplyStrings }.

param
command The text representation of the FTP command to send.
param
args The arguments to the FTP command. If this parameter is set to null, then the command is sent with no argument.
return
The integer value of the FTP reply code returned by the server in response to the command.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        String message;

        __commandBuffer.setLength(0);
        __commandBuffer.append(command);

        if (args != null)
        {
            __commandBuffer.append(' ");
            __commandBuffer.append(args);
        }
        __commandBuffer.append(SocketClient.NETASCII_EOL);

        try{
	    _controlOutput.write(message = __commandBuffer.toString());
        _controlOutput.flush();
        }
        catch (SocketException e)
        {
            if (!isConnected() || !socketIsConnected(_socket_))
            {
                throw new FTPConnectionClosedException("Connection unexpectedly closed.");
            }
            else
            {
                throw e;
            }
        }
	

        if (_commandSupport_.getListenerCount() > 0)
            _commandSupport_.fireCommandSent(command, message);

        __getReply();
        return _replyCode;
    
public voidsetControlEncoding(java.lang.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.

param
encoding The new character encoding for the control connection.

        _controlEncoding = encoding;
    
public intsite(java.lang.String parameters)
A convenience method to send the FTP SITE command to the server, receive the reply, and return the reply code.

param
parameters The site parameters to send.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.SITE, parameters);
    
public intsmnt(java.lang.String dir)
A convenience method to send the FTP SMNT command to the server, receive the reply, and return the reply code.

param
dir The directory name.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.SMNT, dir);
    
private booleansocketIsConnected(java.net.Socket socket)
Checks if the socket is connected using reflection to be backward compatible. The return value of this method is only meaningful in an java 1.4 environment.

param
socket
return
true if connected or pre java 1.4

        if (socket == null)
        {
            return false;
        }

        try
        {
            Method isConnected = socket.getClass().getMethod("isConnected", null);
            return ((Boolean) isConnected.invoke(socket, null)).booleanValue();
        }
        catch (NoSuchMethodException e)
        {
            return true;
        }
        catch (IllegalAccessException e)
        {
            return true;
        }
        catch (InvocationTargetException e)
        {
            return true;
        }
    
public intstat()
A convenience method to send the FTP STAT command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STAT);
    
public intstat(java.lang.String pathname)
A convenience method to send the FTP STAT command to the server, receive the reply, and return the reply code.

param
pathname A pathname to list.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STAT, pathname);
    
public intstor(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The pathname to use for the file when stored at the remote end of the transfer.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STOR, pathname);
    
public intstou()
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STOU);
    
public intstou(java.lang.String pathname)
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, use {@link org.apache.commons.net.ftp.FTPClient} , which will handle all low level details for you.

param
pathname The base pathname to use for the file when stored at the remote end of the transfer. Some FTP servers require this.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STOU, pathname);
    
public intstru(int structure)
A convenience method to send the FTP STRU command to the server, receive the reply, and return the reply code.

param
structure The structure of the file (one of the _STRUCTURE constants).
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.STRU,
                           __modes.substring(structure, structure + 1));
    
public intsyst()
A convenience method to send the FTP SYST command to the server, receive the reply, and return the reply code.

return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.SYST);
    
public inttype(int fileType, int formatOrByteSize)
A convenience method to send the FTP TYPE command for text files to the server, receive the reply, and return the reply code.

param
fileType The type of the file (one of the FILE_TYPE constants).
param
formatOrByteSize The format of the file (one of the _FORMAT constants. In the case of LOCAL_FILE_TYPE, the byte size.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        StringBuffer arg = new StringBuffer();

        arg.append(__modes.charAt(fileType));
        arg.append(' ");
        if (fileType == LOCAL_FILE_TYPE)
            arg.append(formatOrByteSize);
        else
            arg.append(__modes.charAt(formatOrByteSize));

        return sendCommand(FTPCommand.TYPE, arg.toString());
    
public inttype(int fileType)
A convenience method to send the FTP TYPE command to the server, receive the reply, and return the reply code.

param
fileType The type of the file (one of the FILE_TYPE constants).
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.TYPE,
                           __modes.substring(fileType, fileType + 1));
    
public intuser(java.lang.String username)
A convenience method to send the FTP USER command to the server, receive the reply, and return the reply code.

param
username The username to login under.
return
The reply code received from the server.
exception
FTPConnectionClosedException If the FTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send FTP reply code 421. This exception may be caught either as an IOException or independently as itself.
exception
IOException If an I/O error occurs while either sending the command or receiving the server reply.

        return sendCommand(FTPCommand.USER, username);