11
13693261870
2024-11-11 138b959cc11dc9a73b0c766030b99ba1180d8650
src/main/java/com/se/simu/helper/FileHelper.java
@@ -12,13 +12,8 @@
import java.text.DecimalFormat;
import java.util.List;
/**
 * 文件帮助类
 *
 * @author WWW
 * @date 2024-09-12
 */
@Slf4j
@SuppressWarnings("ALL")
public class FileHelper {
    public final static String POINT = ".";
@@ -32,9 +27,6 @@
    public static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    /**
     * 获取文件名
     */
    public static String getFileName(String file) {
        int idx = file.lastIndexOf(File.separator);
        if (idx > -1) {
@@ -44,9 +36,6 @@
        return "";
    }
    /**
     * 获取文件名称
     */
    public static String getName(String file) {
        String fileName = getFileName(file);
        int idx = fileName.lastIndexOf(".");
@@ -57,9 +46,6 @@
        return fileName;
    }
    /**
     * 获取文件扩展名
     */
    public static String getExtension(File file) {
        if (file == null) {
            return null;
@@ -75,9 +61,6 @@
        return fileName.substring(idx);
    }
    /**
     * 获取文件扩展名
     */
    public static String getExtension(String fileName) {
        if (StringHelper.isEmpty(fileName)) {
            return "";
@@ -91,96 +74,6 @@
        return fileName.substring(idx).toLowerCase();
    }
    /**
     * 获取多用途互联网邮件扩展类型
     */
    public static String getMime(String ext) {
        switch (ext) {
            // 图片
            case ".tif":
            case ".tiff":
                return "image/tiff";
            case ".img":
                return "application/x-img";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case ".jpeg":
                return "image/jpeg";
            case ".png":
                return "image/png";
            // 音/视频
            case ".mp3":
                return "audio/mp3";
            case ".mp4":
                return "video/mpeg4";
            case ".avi":
                return "video/avi";
            case ".mpg":
            case ".mpeg":
                return "video/mpg";
            case ".wav":
                return "audio/wav";
            case ".wma":
                return "audio/x-ms-wma";
            case ".swf":
                return "application/x-shockwave-flash";
            case ".wmv":
                return "video/x-ms-wmv";
            case ".rm":
                return "application/vnd.rn-realmedia";
            case ".rmvb":
                return "application/vnd.rn-realmedia-vbr";
            // 网页
            case ".js":
                return "application/x-javascript";
            case ".css":
                return "text/css";
            case ".asp":
                return "text/asp";
            case ".mht":
                return "message/rfc822";
            case ".jsp":
            case ".htm":
            case ".html":
            case ".xhtml":
                return "text/html";
            case ".xml":
            case ".svg":
                return "text/xml";
            // 文件
            case ".txt":
                return "text/plain";
            case ".dbf":
                return "application/x-dbf";
            case ".mdb":
                return "application/msaccess";
            case ".pdf":
                return "application/pdf";
            case ".ppt":
            case ".pptx":
                return "application/x-ppt";
            case ".doc":
            case ".docx":
                return "application/msword";
            case ".xls":
            case ".xlsx":
                return "application/vnd.ms-excel";
            case ".dgn":
                return "application/x-dgn";
            case ".dwg":
                return "application/x-dwg";
            case ".ext":
                return "application/x-msdownload";
            // 默认
            default:
                return "application/octet-stream";
        }
    }
    /**
     * 字节单位换算
     */
    public static String formatByte(long byteNumber) {
        double kbNumber = byteNumber / D1024;
        if (kbNumber < D1024) {
@@ -199,9 +92,6 @@
        return new DecimalFormat("#.##TB").format(tbNumber);
    }
    /**
     * 获取平方米
     */
    public static String getSquareMeter(double num) {
        if (num < I1000000) {
            return new DecimalFormat("#.##平方米").format(num);
@@ -212,9 +102,6 @@
        return new DecimalFormat("#.##平方千米").format(knum);
    }
    /**
     * byte转MB
     */
    public static double sizeToMb(long size) {
        if (size < D1050) {
            return 0.001;
@@ -225,9 +112,6 @@
        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);
@@ -236,9 +120,6 @@
        return md5;
    }
    /**
     * 2.获取快速 MD5 码
     */
    public static String getFastMd5(String filePath) throws IOException {
        String hash = MD5.asHex(MD5.getHash(new File(filePath)));
@@ -248,22 +129,12 @@
        return md5.asHex();
    }
    /**
     * 删除文件夹
     *
     * @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;
@@ -285,12 +156,6 @@
        file.delete();
    }
    /**
     * 获取相对路径
     *
     * @param file 文件
     * @return 相对路径
     */
    public static String getRelativePath(String file) {
        if (StringHelper.isEmpty(file)) {
            return null;
@@ -302,12 +167,6 @@
        return file.substring(start + 1);
    }
    /**
     * 获取路径
     *
     * @param file 文件
     * @return 文件路径
     */
    public static String getPath(String file) {
        if (StringHelper.isEmpty(file)) {
            return null;
@@ -318,9 +177,6 @@
        return file.substring(0, end);
    }
    /**
     * 1.获取文件的MD5
     */
    @SuppressWarnings("unused")
    public static String getFileMd5(String filePath) {
        FileInputStream fis = null;
@@ -353,31 +209,19 @@
        }
    }
    /**
     * 字节码转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 < I16; i++) {
            // 转换成 16 进制字符的转换
            byte byte0 = tmp[i];
            // 取字节中高 4 位的数字转换
            str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf];
            // >>> 为逻辑右移,将符号位一起右移, 取字节中低 4 位的数字转换
            str[k++] = HEX_DIGITS[byte0 & 0xf];
        }
        // 换后的结果转换为字符串
        return new String(str);
    }
    /**
     * 获取字符串的MD5码
     */
    public static String getStringMd5(String text) {
        StringBuilder builder = new StringBuilder();
        try {
@@ -394,9 +238,6 @@
        return builder.toString();
    }
    /**
     * 根据路径获取文件
     */
    public static void getFilesByPath(List<String> list, String path) {
        File file = new File(path);
        if (file.isDirectory()) {
@@ -417,12 +258,6 @@
        }
    }
    /**
     * 复制文件
     *
     * @param src  源文件
     * @param dest 目录文件
     */
    public static void copyFile(File src, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;