⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpcontrolsocket.java

📁 java编写的非常详尽的基于ftp协议的上传下载源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 *
 *  edtFTPj
 *
 *  Copyright (C) 2000-2003 Enterprise Distributed Technologies Ltd
 *
 *  www.enterprisedt.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  Bug fixes, suggestions and comments should be should posted on 
 *  http://www.enterprisedt.com/forums/index.php
 *
 *  Change Log:
 *
 *        $Log: FTPControlSocket.java,v $
 *        Revision 1.44  2008-05-22 04:20:55  bruceb
 *        moved stuff to internal etc
 *
 *        Revision 1.43  2008-05-14 05:51:53  bruceb
 *        added code to cycle through port range and ignore bind exceptions
 *
 *        Revision 1.42  2008-04-02 23:29:21  bruceb
 *        improved "null reply" message
 *
 *        Revision 1.41  2008-03-13 00:21:27  bruceb
 *        421 exception
 *
 *        Revision 1.40  2007-11-13 07:14:04  bruceb
 *        ListenOnAllInterfaces
 *
 *        Revision 1.39  2007-11-07 23:53:14  bruceb
 *        refactoring for FXP
 *
 *        Revision 1.38  2007-10-23 07:20:42  bruceb
 *        fixed doco spelling mistake
 *
 *        Revision 1.37  2007/02/07 23:03:10  bruceb
 *        added close()
 *
 *        Revision 1.36  2007/02/04 23:03:30  bruceb
 *        extra codes and extra debug
 *
 *        Revision 1.35  2007/01/10 02:36:53  bruceb
 *        sendPORTCommand takes a port number now
 *
 *        Revision 1.34  2006/10/27 15:43:23  bruceb
 *        added connect with timeout
 *
 *        Revision 1.33  2006/10/17 10:28:15  bruceb
 *        refactored to get sendPORTCommand()
 *
 *        Revision 1.32  2006/10/11 08:51:44  hans
 *        made cvsId final
 *
 *        Revision 1.31  2006/08/25 20:40:54  hans
 *        Fixed documentation.
 *
 *        Revision 1.30  2006/07/27 14:11:00  bruceb
 *        IPV6 changes (for subclass)
 *
 *        Revision 1.29  2006/05/23 00:17:42  bruceb
 *        apply timeout to active data socket
 *
 *        Revision 1.28  2006/03/09 21:44:24  bruceb
 *        made PASV parsing cleaner
 *
 *        Revision 1.27  2006/02/16 19:47:57  hans
 *        Changed comment
 *
 *        Revision 1.26  2006/02/09 09:00:44  bruceb
 *        fix to allow for missing end bracket re PASV response
 *
 *        Revision 1.25  2005/09/21 08:38:53  bruceb
 *        allow 230 to be initial server response
 *
 *        Revision 1.24  2005/09/02 21:02:44  bruceb
 *        bug fix in readreply
 *
 *        Revision 1.23  2005/08/26 17:48:26  bruceb
 *        passive ip address setting
 *
 *        Revision 1.22  2005/08/04 22:08:42  hans
 *        Remember encoding so that it can be reused when initStreams is called in places other than the constructor
 *
 *        Revision 1.21  2005/06/10 15:43:59  bruceb
 *        message length check
 *
 *        Revision 1.20  2005/06/03 11:26:25  bruceb
 *        comment change
 *
 *        Revision 1.21  2005/05/15 20:44:15  bruceb
 *        removed debug
 *
 *        Revision 1.20  2005/05/15 19:46:28  bruceb
 *        changes for testing setActivePortRange + STOR accepting 350 nonstrict
 *
 *        Revision 1.19  2005/03/26 12:35:45  bruceb
 *        allow for blank lines in server replies
 *
 *        Revision 1.18  2004/11/19 08:28:10  bruceb
 *        added setPORTIP()
 *
 *        Revision 1.17  2004/10/18 15:56:46  bruceb
 *        set encoding for sock, remove sendCommandOld etc
 *
 *        Revision 1.16  2004/09/18 09:33:47  bruceb
 *        1.1.8 tweaks
 *
 *        Revision 1.15  2004/08/31 10:46:59  bruceb
 *        restructured reply code
 *
 *        Revision 1.14  2004/07/23 23:29:57  bruceb
 *        sendcommand public again
 *
 *        Revision 1.13  2004/07/23 08:30:40  bruceb
 *        restructured re non-strict replies
 *
 *        Revision 1.12  2004/05/22 16:52:57  bruceb
 *        message listener
 *
 *        Revision 1.11  2004/05/01 17:05:15  bruceb
 *        Logger stuff added
 *
 *        Revision 1.10  2004/03/23 20:25:47  bruceb
 *        added US-ASCII to control stream constructor
 *
 *        Revision 1.9  2003/11/15 11:23:55  bruceb
 *        changes required for ssl subclasses
 *
 *        Revision 1.6  2003/05/31 14:53:44  bruceb
 *        1.2.2 changes
 *
 *        Revision 1.5  2003/01/29 22:46:08  bruceb
 *        minor changes
 *
 *        Revision 1.4  2002/11/19 22:01:25  bruceb
 *        changes for 1.2
 *
 *        Revision 1.3  2001/10/09 20:53:46  bruceb
 *        Active mode changes
 *
 *        Revision 1.1  2001/10/05 14:42:04  bruceb
 *        moved from old project
 *
 *
 */

package com.enterprisedt.net.ftp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Random;
import java.util.Vector;

import com.enterprisedt.net.ftp.internal.FTPActiveDataSocket;
import com.enterprisedt.net.ftp.internal.FTPDataSocket;
import com.enterprisedt.net.ftp.internal.FTPPassiveDataSocket;
import com.enterprisedt.net.ftp.internal.SocketUtils;
import com.enterprisedt.util.debug.Logger;

/**
 *  Supports client-side FTP operations
 *
 *  @author         Bruce Blackshaw
 *  @version        $Revision: 1.44 $
 *
 */
 public class FTPControlSocket {

     /**
      *  Revision control id
      */
     public static final String cvsId = "@(#)$Id: FTPControlSocket.java,v 1.44 2008-05-22 04:20:55 bruceb Exp $";

     /**
      *   Standard FTP end of line sequence
      */
     static final String EOL = "\r\n";
     
     /**
      * Maximum number of auto retries in active mode
      */
     public static int MAX_ACTIVE_RETRY = 100;

     /**
      *   The default and standard control port number for FTP
      */
     public static final int CONTROL_PORT = 21;
     
     /**
      *   Used to flag messages
      */
     private static final String DEBUG_ARROW = "---> ";
     
     /**
      *   Start of password message
      */
     private static final String PASSWORD_MESSAGE = DEBUG_ARROW + "PASS";
     
     /**
      * Logging object
      */
     private static Logger log = Logger.getLogger("FTPControlSocket");

     /**
      * Use strict return codes if true
      */
     private boolean strictReturnCodes = true;
     
     /**
      * Listen to all interfaces in active mode
      */
     protected boolean listenOnAllInterfaces = true;

     /**
      *  The underlying socket.
      */
     protected Socket controlSock = null;

     /**
      *  The write that writes to the control socket
      */
	 protected Writer writer = null;

     /**
      *  The reader that reads control data from the
      *  control socket
      */
     protected BufferedReader reader = null;
     
     /**
      * Message listener
      */
     private FTPMessageListener messageListener = null;

     /**
      * IP address we force PORT to send - useful with certain
      * NAT configurations
      */
     protected String forcedActiveIP;

     /**
      * Lowest port in active mode port range
      */
     private int lowPort = -1;

     /**
      * Highest port in active mode port range
      */
     private int highPort = -1;

     /**
      * Next port number to use. 0 indicates let Java decide
      */
     private int nextPort = 0;
     
     /**
      * Character encoding.
      */
     private String encoding;
     
     /**
      * The remote address to connect to
      */
     protected InetAddress remoteAddr;
     
     /**
      * If true, uses the original host IP if an internal IP address
      * is returned by the server in PASV mode
      */     
     protected boolean autoPassiveIPSubstitution = false;
     
     /**
      *   Constructor. Performs TCP connection and
      *   sets up reader/writer. Allows different control
      *   port to be used
      *
      *   @param   remoteAddr       Remote inet address
      *   @param   controlPort      port for control stream
      *   @param   timeout          the length of the timeout, in milliseconds
      *   @param   encoding         character encoding used for data
      *   @param   messageListener  listens for messages
      */
     protected FTPControlSocket(InetAddress remoteAddr, int controlPort, int timeout, 
                      String encoding, FTPMessageListener messageListener)
         throws IOException, FTPException {
         
         this(remoteAddr, 
                 SocketUtils.createSocket(remoteAddr, controlPort, timeout), 
                     timeout, encoding, messageListener);
     }

    /**
     * Constructs a new <code>FTPControlSocket</code> using the given
     * <code>Socket</code> object.
     * 
     * @param remoteAddr       the remote address
	 * @param controlSock      Socket to be used. 
	 * @param timeout          Timeout to be used.
     * @param encoding         character encoding used for data
     * @param messageListener  listens for messages
     * 
	 * @throws IOException Thrown if no connection response could be read from the server.
	 * @throws FTPException Thrown if the incorrect connection response was sent by the server.
	 */
	protected FTPControlSocket(InetAddress remoteAddr, Socket controlSock, int timeout, 
                                String encoding, FTPMessageListener messageListener)
		throws IOException, FTPException {
         
        this.remoteAddr = remoteAddr;
		this.controlSock = controlSock;
        this.messageListener = messageListener;
        this.encoding = encoding;
         
		setTimeout(timeout);
		initStreams();
		validateConnection();
    }
    
    /**
     * Set automatic substitution of the remote host IP on if
     * in passive mode
     * 
     * @param autoPassiveIPSubstitution true if set to on, false otherwise
     */
    protected void setAutoPassiveIPSubstitution(boolean autoPassiveIPSubstitution) {
        this.autoPassiveIPSubstitution = autoPassiveIPSubstitution;        
    }

     /**
      *   Checks that the standard 220 reply is returned
      *   following the initiated connection. Allow 230 as some
      *   proxy servers return it
      */
     private void validateConnection()
         throws IOException, FTPException {

         FTPReply reply = readReply();
         String[] validCodes = {"220", "230"};
         validateReply(reply, validCodes);
     }


     /**
      *  Initialize the reader/writer streams for this connection.
      */
     protected void initStreams()
         throws IOException {

         // input stream
         InputStream is = controlSock.getInputStream();
         reader = new BufferedReader(new InputStreamReader(is, encoding));

⌨️ 快捷键说明

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