iodataconnectionfactory.java

来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 475 行 · 第 1/2 页

JAVA
475
字号
     * (non-Javadoc)     *      * @see org.apache.ftpserver.FtpDataConnectionFactory2#getInetAddress()     */    public InetAddress getInetAddress() {        return address;    }    /*     * (non-Javadoc)     *      * @see org.apache.ftpserver.FtpDataConnectionFactory2#getPort()     */    public int getPort() {        return port;    }    /*     * (non-Javadoc)     *      * @see org.apache.ftpserver.FtpDataConnectionFactory2#openConnection()     */    public DataConnection openConnection() throws Exception {        return new IODataConnection(createDataSocket(), session, this);    }    /**     * Get the data socket. In case of error returns null.     */    private synchronized Socket createDataSocket() throws Exception {        // get socket depending on the selection        dataSoc = null;        DataConnectionConfiguration dataConfig = session.getListener()                .getDataConnectionConfiguration();        try {            if (!passive) {                if (secure) {                    LOG.debug("Opening secure active data connection");                    SslConfiguration ssl = getSslConfiguration();                    if (ssl == null) {                        throw new FtpException(                                "Data connection SSL not configured");                    }                    // get socket factory                    SSLContext ctx = ssl.getSSLContext();                    SSLSocketFactory socFactory = ctx.getSocketFactory();                    // create socket                    SSLSocket ssoc = (SSLSocket) socFactory.createSocket();                    ssoc.setUseClientMode(false);                    // initialize socket                    if (ssl.getEnabledCipherSuites() != null) {                        ssoc.setEnabledCipherSuites(ssl.getEnabledCipherSuites());                    }                    dataSoc = ssoc;                } else {                    LOG.debug("Opening active data connection");                    dataSoc = new Socket();                }                dataSoc.setReuseAddress(true);                InetAddress localAddr = resolveAddress(dataConfig                        .getActiveLocalAddress());                // if no local address has been configured, make sure we use the same as the client connects from                if(localAddr == null) {                    localAddr = ((InetSocketAddress)session.getLocalAddress()).getAddress();                }                       SocketAddress localSocketAddress = new InetSocketAddress(localAddr, dataConfig.getActiveLocalPort());                                LOG.debug("Binding active data connection to {}", localSocketAddress);                dataSoc.bind(localSocketAddress);                dataSoc.connect(new InetSocketAddress(address, port));            } else {                if (secure) {                    LOG.debug("Opening secure passive data connection");                    // this is where we wrap the unsecured socket as a SSLSocket. This is                     // due to the JVM bug described in FTPSERVER-241.                    // get server socket factory                    SslConfiguration ssl = getSslConfiguration();                                        // we've already checked this, but let's do it again                    if (ssl == null) {                        throw new FtpException(                                "Data connection SSL not configured");                    }                    SSLContext ctx = ssl.getSSLContext();                    SSLSocketFactory ssocketFactory = ctx.getSocketFactory();                    Socket serverSocket = servSoc.accept();                    SSLSocket sslSocket = (SSLSocket) ssocketFactory                            .createSocket(serverSocket, serverSocket                                    .getInetAddress().getHostName(),                                    serverSocket.getPort(), false);                    sslSocket.setUseClientMode(false);                    // initialize server socket                    if (ssl.getClientAuth() == ClientAuth.NEED) {                        sslSocket.setNeedClientAuth(true);                    } else if (ssl.getClientAuth() == ClientAuth.WANT) {                        sslSocket.setWantClientAuth(true);                    }                    if (ssl.getEnabledCipherSuites() != null) {                        sslSocket.setEnabledCipherSuites(ssl                                .getEnabledCipherSuites());                    }                    dataSoc = sslSocket;                } else {                    LOG.debug("Opening passive data connection");                    dataSoc = servSoc.accept();                }                LOG.debug("Passive data connection opened");            }        } catch (Exception ex) {            closeDataConnection();            LOG.warn("FtpDataConnection.getDataSocket()", ex);            throw ex;        }        dataSoc.setSoTimeout(dataConfig.getIdleTime() * 1000);        // Make sure we initiate the SSL handshake, or we'll        // get an error if we turn out not to send any data        // e.g. during the listing of an empty directory        if (dataSoc instanceof SSLSocket) {            ((SSLSocket) dataSoc).startHandshake();        }        return dataSoc;    }    /*     *  (non-Javadoc)     *   Returns an InetAddress object from a hostname or IP address.     */    private InetAddress resolveAddress(String host)            throws DataConnectionException {        if (host == null) {            return null;        } else {            try {                return InetAddress.getByName(host);            } catch (UnknownHostException ex) {                throw new DataConnectionException("Failed to resolve address", ex);            }        }    }    /*     * (non-Javadoc)     *      * @see org.apache.ftpserver.DataConnectionFactory#isSecure()     */    public boolean isSecure() {        return secure;    }    /**     * Set the security protocol.     */    public void setSecure(final boolean secure) {        this.secure = secure;    }    /*     * (non-Javadoc)     *      * @see org.apache.ftpserver.DataConnectionFactory#isZipMode()     */    public boolean isZipMode() {        return isZip;    }    /**     * Set zip mode.     */    public void setZipMode(final boolean zip) {        isZip = zip;    }    /**     * Check the data connection idle status.     */    public synchronized boolean isTimeout(final long currTime) {        // data connection not requested - not a timeout        if (requestTime == 0L) {            return false;        }        // data connection active - not a timeout        if (dataSoc != null) {            return false;        }        // no idle time limit - not a timeout        int maxIdleTime = session.getListener()                .getDataConnectionConfiguration().getIdleTime() * 1000;        if (maxIdleTime == 0) {            return false;        }        // idle time is within limit - not a timeout        if ((currTime - requestTime) < maxIdleTime) {            return false;        }        return true;    }    /**     * Dispose data connection - close all the sockets.     */    public void dispose() {        closeDataConnection();    }    /**     * Sets the server's control address.     */    public void setServerControlAddress(final InetAddress serverControlAddress) {        this.serverControlAddress = serverControlAddress;    }}

⌨️ 快捷键说明

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