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

📄 ftpdataconnection.java

📁 一个利用Java语言实现的ftp程序
💻 JAVA
字号:
/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package server.ftp;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * We can get the ftp data connection using this class.
 * It uses either PORT or PASV command.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
class FtpDataConnection {
    
    private FtpConfig    mConfig   = null;
    private Socket       mDataSoc  = null;
    private ServerSocket mServSoc  = null;
    
    private InetAddress  mAddress = null;
    private int          miPort   = 0;
    
    private boolean mbPort  = false;
    private boolean mbPasv  = false;
    
    
    /**
     * Constructor.
     * @param cfg ftp config object.
     */
    public FtpDataConnection(FtpConfig cfg) {
        mConfig = cfg;
    }
      
    
    /**
     * Close data socket.
     */
    public void closeDataSocket() {
        
        // close client socket if any
        if(mDataSoc != null) {
            try {
                mDataSoc.close(); 
            } 
            catch(Exception ex) {
                mConfig.getLogger().warn(ex);
            }
            mDataSoc = null;
        }
        
        // close server socket if any
        if (mServSoc != null) {
            try {
               mServSoc.close();
            }
            catch(Exception ex) {
                mConfig.getLogger().warn(ex);
            }
            mServSoc = null;
        }
    }
     
     
    /**
     * Port command.
     */
    public void setPortCommand(InetAddress addr, int port) {
        
        // close old sockets if any
        closeDataSocket();
        
        // set variables
        mbPort = true;
        mbPasv = false;
        mAddress = addr;
        miPort = port;
    } 
    
    
    /**
     * Passive command. It returns the success flag.
     */
    public boolean setPasvCommand() {
        boolean bRet = false;
        
        // close old sockets if any
        closeDataSocket(); 
        
        try {    
            
            // open passive server socket and get parameters            mServSoc = new ServerSocket(getPassivePort(), 1, mConfig.getSelfAddress());            mAddress = mConfig.getSelfAddress();            miPort = mServSoc.getLocalPort(); 
            
            // set different state variables
            mbPort = false;
            mbPasv = true;
            bRet = true;
        }
        catch(Exception ex) {
            mServSoc = null;
            mConfig.getLogger().warn(ex);
        }
        return bRet;
    }
     
     
    /**
     * Get client address.
     */
    public InetAddress getInetAddress() {
        return mAddress;
    }
     
     
    /**
     * Get port number.
     */
    public int getPort() {
        return miPort;
    }
     
     
    /**
     * Get the data socket. In case of error returns null.
     */
    public Socket getDataSocket() {
       
        try {
            
            // get socket depending on the selection
            if(mbPort) {
                mDataSoc = new Socket(mAddress, miPort);                  
            }
            else if(mbPasv) {
                mDataSoc = mServSoc.accept();
            }
        }
        catch(Exception ex) {
            mConfig.getLogger().warn(ex);
            mDataSoc = null;
        }
        
        return mDataSoc;
    }
    
    
    /**
     * Get the passive port. Later we shall use only predefined 
     * port range for passive connections.
     */
    private int getPassivePort() {
        return 0;
    }
    
    
    /**
     * Dispose data connection
     */ 
    public void dispose() {
        closeDataSocket();
    }
              
}
    

⌨️ 快捷键说明

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