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

📄 filemanager.java

📁 CRM源码This file describes some issues that should be implemented in future and how it should be imple
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public static void copyFile( String srcFilePath, File destFile )        throws FileManagerException {        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "Copying file '" + srcFilePath + "'..." );        }        // Check for emptiness.        if( StringHelper.isEmpty( srcFilePath ) ) {            throw new NullPointerException( "Soyrce file path is empty." );        }        if( destFile == null ) {            throw new NullPointerException( "Destination file is null" );        }        try {            // Load file            FileInfo fi = loadFile( srcFilePath );            // Create all subdirectories.            File newDir = destFile.getParentFile();            FileHelper.createDirectory( newDir );            // Save as new            writeFile( destFile, fi );        } catch( IOException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        } catch( java.lang.SecurityException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        }        // Ok.        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "File '" + srcFilePath + "' copied to '" +                         destFile.getAbsolutePath() + "'." );        }    }    /**     * Saves the file.     *     * @param fileInfo FileInfo object     * @param subFolder LOCAL subfolder to store (optional)     * @return File object     * @throws FileManagerException     */    public static File saveFile( FileInfo fileInfo, String subFolder )        throws FileManagerException {        // Emtpiness checks.        if( fileInfo == null ) {            throw new NullPointerException( "File info is null." );        }        String fileName = fileInfo.getFilePath();        if( StringHelper.isEmpty( fileName ) ) {            throw new NullPointerException( "File name is empty." );        }        if( subFolder == null ) {            subFolder = StringHelper.EMPTY_VALUE;        }        // Get file name.        fileName = strip( fileName );        int pos = fileName.lastIndexOf( NAME_SEPARATOR );        if( pos >= 0 ) {            fileName = fileName.substring( pos + 1 );        }        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "File name: '" + fileName + "'." );            logger.INFO( "Subfolder name: '" + subFolder + "'." );        }        File file = null;        try {            // Create local directory if it's not empty and get its full name.            if( !StringHelper.isEmpty( subFolder ) ) {                createDirectory( subFolder );            }            File fileDirectory = getFullFile( subFolder );            // Rename, if the file already exists.            file = new File( fileDirectory, fileName );            if( file.exists() ) {                // Get file extension.                String _fileName = fileName;                String _fileExt = "";                int k = fileName.lastIndexOf( '.' );                if( k >= 0 ) {                    _fileName = fileName.substring( 0, k );                    _fileExt = fileName.substring( k );                }                // File exists, modify file name by adding "_2", "_3", ...                long i = 2;                do {                    String newFileName = _fileName + "_" + ( i++ ) + _fileExt;                    file = new File( fileDirectory, newFileName );                } while( file.exists() && i < Integer.MAX_VALUE );            }            if( !checkFile( file ) ) {                throw new java.lang.SecurityException( "Can't save file '" +                    file.getAbsolutePath() + "' - access denied." );            }            // Write file on the disk.            writeFile( file, fileInfo );        } catch( IOException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        } catch( java.lang.SecurityException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        }        // Ok.        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "File '" + file + "' saved." );        }        return file;    }    /**     * Replaces the existing file.     *     * @param filePath LOCAL file path     * @param fileInfo FileInfo object     * @return File object of new file     * @throws FileManagerException     */    public static File replaceFile( String filePath, FileInfo fileInfo )        throws FileManagerException {        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "Replacing the file '" + filePath + "' ..." );        }        // Emptiness check.        if( StringHelper.isEmpty( filePath ) ) {            throw new NullPointerException( "File path is empty." );        }        if( fileInfo == null ) {            throw new NullPointerException( "File info is null." );        }        File file = null;        try {            file = getFullFile( filePath );            if( !checkFile( file ) ) {                throw new java.lang.SecurityException( "Can't replace file '" +                    file.getAbsolutePath() + "' - access denied." );            }            // Overwrite the file on disk.            writeFile( file, fileInfo );        } catch( IOException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        } catch( java.lang.SecurityException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        }        // Ok.        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "File '" + file + "' replaced." );        }        return file;    }    /**     * Deletes the existing file.     * @param filePath LOCAL file path     * @throws FileManagerException     */    public static void deleteFile( String filePath )        throws FileManagerException {        if( logger.getLogger().isInfoEnabled() ) {            logger.INFO( "Deleting the file '" + filePath + "' ..." );        }        // Check for empty path.        if( StringHelper.isEmpty( filePath ) ) {            throw new NullPointerException( "File path is empty." );        }        try {            File file = getFullFile( filePath );            if( !checkFile( file ) ) {                throw new java.lang.SecurityException( "Can't delete file '" +                    file.getAbsolutePath() + "' - access denied." );            }            // Delete the file if it exists.            FileHelper.deleteFile( file );        } catch( IOException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        } catch( java.lang.SecurityException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        }    }    /**     * Gets the LOCAL (AKA relative to upload directory) name for the     * given LOCAL directiry name and LOCAL file name.     *     * @param fileName LOCAL file name     * @param subFolder LOCAL subfolder (optional)     * @return LOCAL name (subFolder + NAME_SEPARATOR + fileName)     * @throws FileManagerException     */    public static String getLocalName( String fileName, String subFolder )        throws FileManagerException {        // Check for emptiness.        if( StringHelper.isEmpty( fileName ) ) {            throw new NullPointerException( "File name is empty." );        }        // Ok.        if( StringHelper.isEmpty( subFolder ) ) {            return fileName;        } else {            return subFolder + NAME_SEPARATOR + fileName;        }    }    /**     * Gets the LOCAL name for the given file.     *     * @param file given File object     * @return LOCAL name     * @throws FileManagerException     */    public static String getLocalName( File file )        throws FileManagerException {        // Check file.        if( file == null ) {            throw new NullPointerException( "File is null." );        }        try {            String fileAbsPath = file.getCanonicalPath();            String fsdirAbsPath = getFSDirectory().getCanonicalPath();            if( !fileAbsPath.startsWith( fsdirAbsPath ) ) {                throw new IllegalStateException( "File '" + fileAbsPath +                                                 "' is not under the upload directory." );            }            String filePath = strip( fileAbsPath.substring( fsdirAbsPath.length() ) );            if( filePath.indexOf( NAME_SEPARATOR ) == 0 ) {                filePath = filePath.substring( 1 );            }            return filePath;        } catch( IOException ex ) {            logger.ERROR( ex );            throw new FileManagerException( ex );        }    }    /**     * Builds system independent file name.     * @param s some file name     * @return system file name     */    public static String strip( String s ) {        return s.replace( '\\', NAME_SEPARATOR );    }    //    // Gets the full name of the given file.    //    public static File getFullFile( String filePath ) {        if( StringHelper.isEmpty( filePath ) ) {            return getFSDirectory();        } else {            return new File( getFSDirectory(), strip( filePath ) );        }    }    //    // Gets the file with the full name.    //    public static File getFullFile( String dirPath, String filePath ) {        return new File( getFullFile( dirPath ), strip( filePath ) );    }    // ========================================================= Private methods    //    // Writes file to disk.    //    private static void writeFile( File file, FileInfo fileInfo )        throws IOException {        FileHelper.writeFile( file, fileInfo.getData() );    }    //    // Checks if the given file is under the file storage directory.    //    private static boolean checkFile( File file ) {        try {            getLocalName( file );            return true;        } catch( Exception ex ) {            logger.ERROR( ex );        }        return false;    }    // =========================================================== Inner classes    /**     * Stores the file information.     *     * @author [ALB] Baranov Andrey     * @version 1.0     */    public static final class FileInfo {        private String filePath;        private byte[] data;        public FileInfo( String filePath, byte[] data ) {            this.filePath = filePath;            this.data = data;        }        public String getFilePath() {            return filePath;        }        public byte[] getData() {            return data;        }    }}

⌨️ 快捷键说明

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