管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-03-21 b20fd29dc3864405af4afa0aeb99d13d74fbfcdc
src/main/java/com/lf/server/helper/FileHelper.java
@@ -7,11 +7,10 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.List;
@@ -25,9 +24,6 @@
    /**
     * 获取文件名
     *
     * @param file
     * @return
     */
    public static String getFileName(String file) {
        int idx = file.lastIndexOf(File.separator);
@@ -36,6 +32,19 @@
        }
        return "";
    }
    /**
     * 获取文件名称
     */
    public static String getName(String file) {
        String fileName = getFileName(file);
        int idx = fileName.lastIndexOf(".");
        if (idx > -1) {
            return fileName.substring(0, idx);
        }
        return fileName;
    }
    /**
@@ -48,7 +57,7 @@
        String fileName = file.getName().toLowerCase();
        int idx = fileName.lastIndexOf(StaticData.POINT);
        int idx = fileName.indexOf(StaticData.POINT);
        if (idx == -1) {
            return "";
        }
@@ -74,9 +83,6 @@
    /**
     * 获取多用途互联网邮件扩展类型
     *
     * @param ext 文件扩展名
     * @return
     */
    public static String getMime(String ext) {
        switch (ext) {
@@ -184,6 +190,37 @@
    }
    /**
     * 获取文件大小
     */
    public static String getSizes(double mbNumber) {
        if (mbNumber < StaticData.D1024) {
            return new DecimalFormat("#.##MB").format(mbNumber);
        }
        double gbNumber = mbNumber / StaticData.D1024;
        if (gbNumber < StaticData.D1024) {
            return new DecimalFormat("#.##GB").format(gbNumber);
        }
        double tbNumber = gbNumber / StaticData.D1024;
        return new DecimalFormat("#.##TB").format(tbNumber);
    }
    /**
     * 获取平方米
     */
    public static String getSquareMeter(double num) {
        if (num < StaticData.I1000000) {
            return new DecimalFormat("#.##平方米").format(num);
        }
        double knum = num / StaticData.I1000000;
        return new DecimalFormat("#.##平方千米").format(knum);
    }
    /**
     * byte转MB
     */
    public static double sizeToMb(long size) {
@@ -197,9 +234,9 @@
    }
    /**
     * 获取文件 MD5 码
     * 3.获取文件MD5码(JDK)
     */
    public static String getFileMd5(String filePath) throws IOException {
    public static String getMd5ByJdk(String filePath) throws IOException {
        FileInputStream fileStream = new FileInputStream(filePath);
        String md5 = DigestUtils.md5Hex(fileStream);
        fileStream.close();
@@ -208,7 +245,7 @@
    }
    /**
     * 获取快速 MD5 码
     * 2.获取快速 MD5 码
     */
    public static String getFastMd5(String filePath) throws IOException {
        String hash = MD5.asHex(MD5.getHash(new File(filePath)));
@@ -252,17 +289,19 @@
     * @param file 文件
     */
    public static void deleteFiles(File file) {
        if (file == null || !file.exists()) {
        if (null == file || !file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFiles(f);
                } else {
                    f.delete();
            if (null != files && files.length > 0) {
                for (File f : files) {
                    if (f.isDirectory()) {
                        deleteFiles(f);
                    } else {
                        f.delete();
                    }
                }
            }
        }
@@ -304,17 +343,15 @@
    }
    /**
     * 获取文件的MD5
     * @param file
     * @return
     * 1.获取文件的MD5
     */
    @SuppressWarnings("unused")
    public static String getFileMd5(File file) {
    public static String getFileMd5(String filePath) {
        FileInputStream fis = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);
            fis = new FileInputStream(new File(filePath));
            FileChannel fChannel = fis.getChannel();
            ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024);
@@ -340,6 +377,9 @@
        }
    }
    /**
     * 字节码转16进制
     */
    public static String byteToHexString(byte[] tmp) {
        // 每个字节用 16 进制表示的话,使用两个字符,
        char[] str = new char[16 * 2];
@@ -358,4 +398,71 @@
        // 换后的结果转换为字符串
        return new String(str);
    }
    /**
     * 获取字符串的MD5码
     */
    public static String getStringMd5(String text) {
        StringBuilder builder = new StringBuilder();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(text.getBytes(StandardCharsets.UTF_8));
            for (byte aByte : bytes) {
                builder.append(Integer.toHexString((0x000000FF & aByte) | 0xFFFFFF00).substring(6));
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return builder.toString();
    }
    /**
     * 根据路径获取文件
     */
    public static void getFilesByPath(List<String> list, String path) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (null == files) {
                return;
            }
            for (File f : files) {
                if (f.isDirectory()) {
                    getFilesByPath(list, f.getPath());
                } else {
                    list.add(f.getPath());
                }
            }
        } else {
            list.add(file.getPath());
        }
    }
    /**
     * 复制文件
     *
     * @param src  源文件
     * @param dest 目录文件
     */
    public static void copyFile(File src, File dest) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            os.close();
            is.close();
        }
    }
}