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

📄 ftphelper.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        /**     * Sends an FTP Server site specific command     *     * @param args site command arguments     * @throws IOException on I/O errors     */    public void sendSiteCommand( String args )        throws IOException {        if( ftp.isConnected() ) {            try {                ftp.sendSiteCommand( args );            } catch( IOException ex ) {}        }    }    /**     * Disconnects from the FTP server     *     * @throws IOException on I/O errors     */    public void disconnect()        throws IOException {        if( ftp.isConnected() ) {            try {                ftp.logout();                ftp.disconnect();                is_connected = false;            } catch( IOException ex ) {}        }    }    /**     * Makes the full name of the file on the FTP server by joining its path     * and the local file name.     *     * @param ftpPath file path on the server     * @param localFile local file     * @return full name of the file on the FTP server     */    public String makeFTPFileName( String ftpPath, File localFile ) {        if( StringHelper.isEmpty( ftpPath ) ) {            return localFile.getName();        } else {            String path = ftpPath.trim();            if( path.charAt( path.length() - 1 ) != '/' ) {                path = path + "/";            }            return path + localFile.getName();        }    }    /**     * Test coonection to ftp server     *     * @return true, if connected     */    public boolean isConnected() {        return is_connected;    }    /**     * Get current directory on ftp server     *     * @return current directory     */    public String getWorkingDirectory() {        if( !is_connected ) {            return "";        }        try {            return ftp.printWorkingDirectory();        } catch( IOException e ) {}        return "";    }    /**     * Set working directory on ftp server     *     * @param dir new working directory     * @return true, if working directory changed     */    public boolean setWorkingDirectory( String dir ) {        if( !is_connected ) {            return false;        }        try {            return ftp.changeWorkingDirectory( dir );        } catch( IOException e ) {}        return false;    }    /**     * Change working directory on ftp server to parent directory     *     * @return true, if working directory changed     */    public boolean setParentDirectory() {        if( !is_connected ) {            return false;        }        try {            return ftp.changeToParentDirectory();        } catch( IOException e ) {}        return false;    }    /**     * Get parent directory name on ftp server     *     * @return parent directory     */    public String getParentDirectory() {        if( !is_connected ) {            return "";        }        String w = getWorkingDirectory();        setParentDirectory();        String p = getWorkingDirectory();        setWorkingDirectory( w );        return p;    }    /**     * Get directory contents on ftp server     *     * @param filePath directory     * @return list of FTPFileInfo structures     * @throws IOException     */    public List listFiles( String filePath )        throws IOException {        List fileList = new ArrayList();        FTPFile[] ftpFiles = ftp.listFiles( filePath );        int size = ( ftpFiles == null ) ? 0 : ftpFiles.length;        for( int i = 0; i < size; i++ ) {            FTPFile ftpFile = ftpFiles[i];            FTPFileInfo fi = new FTPFileInfo();            fi.setName( ftpFile.getName() );            fi.setSize( ftpFile.getSize() );            fi.setTimestamp( ftpFile.getTimestamp() );            fi.setType( ftpFile.isDirectory() );            fileList.add( fi );        }        return fileList;    }    /**     * Get file from ftp server into given output stream     *     * @param ftpFileName file name on ftp server     * @param out OutputStream     * @throws IOException     */    public void getFile( String ftpFileName, OutputStream out )        throws IOException {        try {            // Get file info.            FTPFile[] fileInfoArray = ftp.listFiles( ftpFileName );            if( fileInfoArray == null ) {                throw new FileNotFoundException( "File '" + ftpFileName + "' was not found on FTP server." );            }            // Check file size.            FTPFile fileInfo = fileInfoArray[0];            long size = fileInfo.getSize();            if( size > Integer.MAX_VALUE ) {                throw new IOException( "File '" + ftpFileName + "' is too large." );            }            // Download file.            if( !ftp.retrieveFile( ftpFileName, out ) ) {                throw new IOException( "Error loading file '"                                       + ftpFileName                                       + "' from FTP server. Check FTP permissions and path." );            }            out.flush();        } finally {            if( out != null ) {                try {                    out.close();                } catch( IOException ex ) {}            }        }    }    /**     * Put file on ftp server from given input stream     *     * @param ftpFileName file name on ftp server     * @param in InputStream     * @throws IOException     */    public void putFile( String ftpFileName, InputStream in )        throws IOException {        try {            // Use passive mode to pass firewalls.            ftp.enterLocalPassiveMode();            if( !ftp.storeFile( ftpFileName, in ) ) {                throw new IOException( "Can't upload file '"                                       + ftpFileName                                       + "' to FTP server. Check FTP permissions and path." );            }        } finally {            try {                in.close();            } catch( IOException ex ) {}        }    }    // ============================================================ Inner class    /**     * File info class, containing information about file on ftp server. Has     * only getters and setters methods     */    public static class FTPFileInfo        implements Serializable {        String name;        long size;        Calendar timestamp;        boolean is_dir;        public String getName() {            return name;        }        public void setName( String name ) {            this.name = name;        }        public long getSize() {            return size;        }        public void setSize( long size ) {            this.size = size;        }        public Calendar getTimestamp() {            return timestamp;        }        public void setTimestamp( Calendar timestamp ) {            this.timestamp = timestamp;        }        public boolean isDirectory() {            return is_dir;        }        public void setType( boolean is_dir ) {            this.is_dir = is_dir;        }    }}

⌨️ 快捷键说明

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