| | |
| | | import java.text.DecimalFormat; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 文件帮助类 |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class FileHelper { |
| | | private final static Log log = LogFactory.getLog(FileHelper.class); |
| | | |
| | | /** |
| | | * 获取文件名 |
| | | * |
| | | * @param file |
| | | * @return |
| | | */ |
| | | public static String getFileName(String file) { |
| | | int idx = file.lastIndexOf(File.separator); |
| | | if (idx > -1) { |
| | |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 获取文件扩展名 |
| | | */ |
| | | public static String getExtension(File file) { |
| | | if (file == null) { |
| | | return null; |
| | |
| | | return fileName.substring(idx); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件扩展名 |
| | | */ |
| | | public static String getExtension(String fileName) { |
| | | if (StringHelper.isEmpty(fileName)) { |
| | | return ""; |
| | |
| | | return fileName.substring(idx).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * 获取多用途互联网邮件扩展类型 |
| | | * |
| | | * @param ext 文件扩展名 |
| | | * @return |
| | | */ |
| | | public static String getMime(String ext) { |
| | | switch (ext) { |
| | | // 图片 |
| | | case ".tif": |
| | | case ".tiff": |
| | | return "image/tiff"; |
| | |
| | | return "image/jpeg"; |
| | | case ".png": |
| | | return "image/png"; |
| | | // 音/视频 |
| | | case ".mp3": |
| | | return "audio/mp3"; |
| | | case ".mp4": |
| | |
| | | return "application/vnd.rn-realmedia"; |
| | | case ".rmvb": |
| | | return "application/vnd.rn-realmedia-vbr"; |
| | | // 网页 |
| | | case ".js": |
| | | return "application/x-javascript"; |
| | | case ".css": |
| | |
| | | case ".xml": |
| | | case ".svg": |
| | | return "text/xml"; |
| | | // 文件 |
| | | case ".txt": |
| | | return "text/plain"; |
| | | case ".dbf": |
| | |
| | | return "application/x-dwg"; |
| | | case ".ext": |
| | | return "application/x-msdownload"; |
| | | // 默认 |
| | | default: |
| | | return "application/octet-stream"; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 字节单位换算 |
| | | */ |
| | | public static String formatByte(long byteNumber) { |
| | | double kbNumber = byteNumber / StaticData.D1024; |
| | | if (kbNumber < StaticData.D1024) { |
| | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件大小 |
| | | */ |
| | | public static String getSizes(double mbNumber) { |
| | | if (mbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##MB").format(mbNumber); |
| | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | | |
| | | /** |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | | if (size < StaticData.D1050) { |
| | | return 0.001; |
| | |
| | | return Double.parseDouble(str); |
| | | } |
| | | |
| | | /** |
| | | * 3.获取文件MD5码(JDK) |
| | | */ |
| | | public static String getMd5ByJdk(String filePath) throws IOException { |
| | | FileInputStream fileStream = new FileInputStream(filePath); |
| | | String md5 = DigestUtils.md5Hex(fileStream); |
| | |
| | | return md5; |
| | | } |
| | | |
| | | /** |
| | | * 2.获取快速 MD5 码 |
| | | */ |
| | | public static String getFastMd5(String filePath) throws IOException { |
| | | String hash = MD5.asHex(MD5.getHash(new File(filePath))); |
| | | |
| | |
| | | return md5.asHex(); |
| | | } |
| | | |
| | | /** |
| | | * 删除文件 |
| | | */ |
| | | public static void deleteFiles(List<MetaFileEntity> list) { |
| | | try { |
| | | for (MetaFileEntity mf : list) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 删除文件夹 |
| | | * |
| | | * @param dir 文件夹 |
| | | */ |
| | | public static void deleteDir(String dir) { |
| | | File file = new File(dir); |
| | | |
| | | deleteFiles(file); |
| | | } |
| | | |
| | | /** |
| | | * 级联删除文件 |
| | | * |
| | | * @param file 文件 |
| | | */ |
| | | public static void deleteFiles(File file) { |
| | | if (null == file || !file.exists()) { |
| | | return; |
| | |
| | | file.delete(); |
| | | } |
| | | |
| | | /** |
| | | * 获取相对路径 |
| | | * |
| | | * @param file 文件 |
| | | * @return 相对路径 |
| | | */ |
| | | public static String getRelativePath(String file) { |
| | | if (StringHelper.isEmpty(file)) { |
| | | return null; |
| | |
| | | return file.substring(start + 1); |
| | | } |
| | | |
| | | /** |
| | | * 获取路径 |
| | | * |
| | | * @param file 文件 |
| | | * @return 文件路径 |
| | | */ |
| | | public static String getPath(String file) { |
| | | if (StringHelper.isEmpty(file)) { |
| | | return null; |
| | |
| | | return file.substring(0, end); |
| | | } |
| | | |
| | | /** |
| | | * 1.获取文件的MD5 |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public static String getFileMd5(String filePath) { |
| | | FileInputStream fis = null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 字节码转16进制 |
| | | */ |
| | | public static String byteToHexString(byte[] tmp) { |
| | | // 每个字节用 16 进制表示的话,使用两个字符, |
| | | char[] str = new char[16 * 2]; |
| | | |
| | | // 所以表示成 16 进制需要 32 个字符,表示转换结果中对应的字符位置 |
| | | int k = 0; |
| | | // 从第一个字节开始,对 MD5 的每一个字节 |
| | | for (int i = 0; i < StaticData.I16; i++) { |
| | | // 转换成 16 进制字符的转换 |
| | | byte byte0 = tmp[i]; |
| | | // 取字节中高 4 位的数字转换 |
| | | str[k++] = StaticData.HEX_DIGITS[byte0 >>> 4 & 0xf]; |
| | | // >>> 为逻辑右移,将符号位一起右移, 取字节中低 4 位的数字转换 |
| | | str[k++] = StaticData.HEX_DIGITS[byte0 & 0xf]; |
| | | } |
| | | // 换后的结果转换为字符串 |
| | | return new String(str); |
| | | } |
| | | |
| | | /** |
| | | * 获取字符串的MD5码 |
| | | */ |
| | | public static String getStringMd5(String text) { |
| | | StringBuilder builder = new StringBuilder(); |
| | | try { |
| | |
| | | return builder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 根据路径获取文件 |
| | | */ |
| | | public static void getFilesByPath(List<String> list, String path) { |
| | | File file = new File(path); |
| | | if (file.isDirectory()) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制文件 |
| | | * |
| | | * @param src 源文件 |
| | | * @param dest 目录文件 |
| | | */ |
| | | public static void copyFile(File src, File dest) throws IOException { |
| | | InputStream is = null; |
| | | OutputStream os = null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 过滤输入字符串, 剔除多行注释以及替换掉反斜杠 |
| | | */ |
| | | public static String filter(String str) { |
| | | return str.replaceAll("/\\*[\\s\\S]*?\\*/", ""); |
| | | } |
| | | |
| | | /** |
| | | * 解码 |
| | | */ |
| | | public static String decode(String str) { |
| | | try { |
| | | return URLDecoder.decode(str, "UTF-8"); |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取ClassPath-jar |
| | | */ |
| | | public static String getClassPathForJar() { |
| | | URL resource = ClassLoader.getSystemResource(""); |
| | | if (null == resource) { |
| | |
| | | return decode(filter(resource.getPath())); |
| | | } |
| | | |
| | | /** |
| | | * 获取ClassPath-war |
| | | */ |
| | | public static String getClassPathForWar() throws IOException { |
| | | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); |
| | | |