ftp.java

来自「apache推出的net包」· Java 代码 · 共 1,475 行 · 第 1/5 页

JAVA
1,475
字号
                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.");    }    // initiates control connections and gets initial reply    protected void _connectAction_() throws IOException    {        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();    }    /**     * 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.     */    public void setControlEncoding(String encoding) {        _controlEncoding = encoding;    }    /**     * @return The character encoding used to communicate over the     * control connection.     */    public String getControlEncoding() {        return _controlEncoding;    }    /***     * Adds a ProtocolCommandListener.  Delegates this task to     * {@link #_commandSupport_  _commandSupport_ }.     * <p>     * @param listener  The ProtocolCommandListener to add.     ***/    public void addProtocolCommandListener(ProtocolCommandListener listener)    {        _commandSupport_.addProtocolCommandListener(listener);    }    /***     * Removes a ProtocolCommandListener.  Delegates this task to     * {@link #_commandSupport_  _commandSupport_ }.     * <p>     * @param listener  The ProtocolCommandListener to remove.     ***/    public void removeProtocolCommandListener(ProtocolCommandListener listener)    {        _commandSupport_.removeProtocolCommandListener(listener);    }    /***     * 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.     * <p>     * @exception IOException If an error occurs while disconnecting.     ***/    public void disconnect() throws IOException    {        super.disconnect();        _controlInput = null;        _controlOutput = null;        _replyLines.setSize(0);        _newReplyString = false;        _replyString = null;    }    /***     * 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 }.     * <p>     * @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.     ***/    public int sendCommand(String command, String args) throws IOException    {        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;    }    /**     * 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     */    private boolean socketIsConnected(Socket socket)    {        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;        }    }    /***     * 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 }.     * <p>     * @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.     ***/    public int sendCommand(int command, String args) throws IOException    {        return sendCommand(FTPCommand._commands[command], args);    }    /***     * 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 }.     * <p>     * @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.     ***/    public int sendCommand(String command) throws IOException    {        return sendCommand(command, null);    }    /***     * 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 }.     * <p>     * @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.     ***/    public int sendCommand(int command) throws IOException    {        return sendCommand(command, null);    }    /***     * 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     * <code> connect </code> is of type void.     * <p>     * @return The integer value of the reply code of the last FTP reply.     ***/    public int getReplyCode()    {        return _replyCode;    }    /***     * 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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?