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

📄 filetransferclient.java

📁 java编写的非常详尽的基于ftp协议的上传下载源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**
     * Set the password of the user to log in with. Can only do this if
     * not already connected.
     * 
     * @param password          password to log in with.
     * @throws FTPException
     */
    public synchronized void setPassword(String password) throws FTPException {
        checkConnection(false);
        masterContext.setPassword(password);
    }
    
    /**
     * Get the current user name.
     * 
     * @return current user name
     */
    public synchronized String getUserName() {
        return masterContext.getUserName();
    }
    
    /**
     * Get the advanced FTP configuration parameters object
     * 
     * @return advanced parameters
     */
    public synchronized AdvancedFTPSettings getAdvancedFTPSettings() {
        return advancedFTPSettings;        
    }
    
    /**
     * Get the advanced general configuration parameters object, for none
     * protocol specific parameters
     * 
     * @return advanced parameters
     */
    public synchronized AdvancedGeneralSettings getAdvancedSettings() {
        return advancedSettings;        
    }
    
    /**
     * Set the event listener for transfer event notification
     * 
     * @param listener  event listener reference
     */
    public synchronized void setEventListener(EventListener listener) {
        this.listener = listener;
        eventAggregator = new EventAggregator(listener);
        if (ftpClient != null) {
            eventAggregator.setConnId(ftpClient.getId());       
            ftpClient.setMessageListener(eventAggregator);
            ftpClient.setProgressMonitor(eventAggregator);
            ftpClient.setProgressMonitorEx(eventAggregator);
        }
    }
    
    /**
     * Make a connection to the FTP server. 
     * 
     * @throws FTPException 
     * @throws IOException 
     */
    public synchronized void connect() throws FTPException, IOException {
        // events
        if (eventAggregator != null) {
            eventAggregator.setConnId(ftpClient.getId());
        
            ftpClient.setMessageListener(eventAggregator);
            ftpClient.setProgressMonitor(eventAggregator);
            ftpClient.setProgressMonitorEx(eventAggregator);
        }
        
        //stats
        statistics.clear();
        
        configureClient();
        log.debug("Configured client");
        ftpClient.connect();
        log.debug("Client connected");
        if (masterContext.isAutoLogin()) {
            log.debug("Logging in");
            ftpClient.login(masterContext.getUserName(), masterContext.getPassword());
            log.debug("Logged in");
            configureTransferType(masterContext.getContentType());
        }
        else {
            log.debug("Manual login enabled");
        }
    }
    
    /**
     * Perform a manual login using the credentials that have been set. This
     * should only be performed if auto login is disabled - normally connect()
     * performs the login automatically.
     * 
     * @throws FTPException
     * @throws IOException
     */
    public void manualLogin() throws FTPException, IOException {
        checkConnection(true);
        log.debug("Logging in");
        ftpClient.login(masterContext.getUserName(), masterContext.getPassword());
        log.debug("Logged in");
        configureTransferType(masterContext.getContentType());
    }
    
    /**
     * Apply the master context's settings to the client
     * 
     * @throws IOException
     * @throws FTPException
     */
    private void configureClient() throws IOException, FTPException {
        ftpClient.setRemoteHost(masterContext.getRemoteHost());
        ftpClient.setRemotePort(masterContext.getRemotePort());
        ftpClient.setTimeout(masterContext.getTimeout());       
        ftpClient.setControlEncoding(masterContext.getControlEncoding());
        ftpClient.setStrictReturnCodes(masterContext.isStrictReturnCodes());
        ftpClient.setDetectTransferMode(masterContext.getDetectContentType());
        ftpClient.setConnectMode(masterContext.getConnectMode());
        ftpClient.setParserLocales(masterContext.getParserLocales());
        ftpClient.setAutoPassiveIPSubstitution(masterContext.isAutoPassiveIPSubstitution());
        ftpClient.setDeleteOnFailure(masterContext.isDeleteOnFailure());
        ftpClient.setActiveIPAddress(masterContext.getActiveIPAddress());
        ftpClient.setMonitorInterval(masterContext.getTransferNotifyInterval());
        ftpClient.setTransferBufferSize(masterContext.getTransferBufferSize());
        ftpClient.setFileNotFoundMessages(masterContext.getFileNotFoundMessages());
        ftpClient.setDirectoryEmptyMessages(masterContext.getDirectoryEmptyMessages());
        ftpClient.setTransferCompleteMessages(masterContext.getTransferCompleteMessages());
        if (masterContext.getActiveHighPort() >= 0 && masterContext.getActiveLowPort() >= 0)
            ftpClient.setActivePortRange(masterContext.getActiveLowPort(), masterContext.getActiveHighPort());
       
    }
    
    
    private void configureTransferType(FTPTransferType type) 
        throws IOException, FTPException {
        ftpClient.setDetectTransferMode(masterContext.getDetectContentType());
        ftpClient.setType(type);
    }
    
    private void checkTransferSettings() throws FTPException {
        
        if (ftpClient.getDetectTransferMode() != masterContext.getDetectContentType())
            ftpClient.setDetectTransferMode(masterContext.getDetectContentType());
        
        if (ftpClient.isStrictReturnCodes() != masterContext.isStrictReturnCodes())
            ftpClient.setStrictReturnCodes(masterContext.isStrictReturnCodes());
        
        if (!ftpClient.getConnectMode().equals(masterContext.getConnectMode()))
            ftpClient.setConnectMode(masterContext.getConnectMode());        
        
        if (ftpClient.isAutoPassiveIPSubstitution() != masterContext.isAutoPassiveIPSubstitution())
            ftpClient.setAutoPassiveIPSubstitution(masterContext.isAutoPassiveIPSubstitution());
        
        if (ftpClient.isDeleteOnFailure() != masterContext.isDeleteOnFailure())
            ftpClient.setDeleteOnFailure(masterContext.isDeleteOnFailure());
        
        if (ftpClient.getActiveIPAddress() != masterContext.getActiveIPAddress())
            ftpClient.setActiveIPAddress(masterContext.getActiveIPAddress());
        
        if (ftpClient.getTransferBufferSize() != masterContext.getTransferBufferSize())
            ftpClient.setTransferBufferSize(masterContext.getTransferBufferSize());
        
        if (ftpClient.getMonitorInterval() != masterContext.getTransferNotifyInterval())
            ftpClient.setMonitorInterval(masterContext.getTransferNotifyInterval());  
        
        if (masterContext.getActiveHighPort() != ftpClient.getActiveHighPort() ||
            masterContext.getActiveLowPort() != ftpClient.getActiveLowPort())
            ftpClient.setActivePortRange(masterContext.getActiveLowPort(), masterContext.getActiveHighPort());
    }
    
    private void checkListingSettings() throws FTPException {
        ftpClient.setParserLocales(masterContext.getParserLocales());
        checkTransferSettings();
    }
    
    
    /**
     * Get statistics on file transfers and deletions. 
     * 
     * @return FTPStatistics
     */
    public synchronized FileStatistics getStatistics() {
        return statistics;
    }
    
    /**
     * Request that the remote server execute the literal command supplied. In
     * FTP, this is the equivalent of 'quote'. It could be used to send a SITE
     * command to the server, e.g. "SITE recfm=FB lrecl=180 blksize=5400". 
     *  
     * @param command   command string
     * @return result string returned by server
     * @throws FTPException
     * @throws IOException
     */
    public synchronized String executeCommand(String command) 
        throws FTPException, IOException {
        return ftpClient.quote(command);
    }
    
    /**
     * Cancel current transfer if underway
     */
    public void cancelAllTransfers() {

        log.debug("cancelAllTransfers() called");
        ftpClient.cancelTransfer();
    }
    
    /**
     * Get a string that represents the remote system that the client is logged
     * into.
     * 
     * @return system string
     * @throws FTPException
     * @throws IOException
     */
    public synchronized String getSystemType() 
        throws FTPException, IOException {
        return ftpClient.system();
    }
    
    /**
     * List the names of files and directories in the current directory on the FTP server.
     * 
     * @return String[]
     * @throws FTPException, IOException 
     */
    public synchronized String[] directoryNameList() 
        throws FTPException, IOException {
        return directoryNameList("", false);
    }

    
    /**
     * List the names of files and directories of a directory on the FTP server.
     * 
     * @param directoryName    name of the directory (generally not a path). Some 
     *                          servers will accept a wildcard.
     * @param isLongListing    true if the listing is a long format listing
     * @return String[]
     * @throws FTPException, IOException 
     */
    public synchronized String[] directoryNameList(String directoryName, boolean isLongListing) 
        throws FTPException, IOException {
        checkListingSettings();
        return ftpClient.dir(directoryName, isLongListing);
    }
    
    /**
     * List the current directory on the FTP server.
     * 
     * @throws FTPException, IOException 
     * @throws ParseException 
     */
    public synchronized FTPFile[] directoryList() 
        throws FTPException, IOException, ParseException {
        return directoryList("");
    }
   
    /**
     * List a directory on the FTP server.
     * 
     * @param directoryName    name of the directory (generally not a path). Some 
     *                          servers will accept a wildcard.
     * @throws FTPException, IOException 
     * @throws ParseException 
     */
    public synchronized FTPFile[] directoryList(String directoryName) 
        throws FTPException, IOException, ParseException {
        checkListingSettings();
        return ftpClient.dirDetails(directoryName);
    }
    
    /**
     * Download a file from the FTP server into a byte array.
     * 
     * @param remoteFileName   name of the remote file to be downloaded
     * @throws FTPException 
     */
    public synchronized byte[] downloadByteArray(String remoteFileName) throws FTPException, IOException {
        checkTransferSettings();
        return ftpClient.get(remoteFileName); 
    }
    
    /**
     * Download a file from the FTP server .

⌨️ 快捷键说明

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