| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.entity.data.MetaEntity; |
| | | import com.lf.server.entity.data.MetaFileEntity; |
| | | import com.twmacinta.util.MD5; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | |
| | | import java.io.IOException; |
| | | 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; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 文件帮助类 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件大小 |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件MD5码(JDK) |
| | | * 3.获取文件MD5码(JDK) |
| | | */ |
| | | public static String getMd5ByJdk(String filePath) throws IOException { |
| | | FileInputStream fileStream = new FileInputStream(filePath); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取快速 MD5 码 |
| | | * 2.获取快速 MD5 码 |
| | | */ |
| | | public static String getFastMd5(String filePath) throws IOException { |
| | | String hash = MD5.asHex(MD5.getHash(new File(filePath))); |
| | |
| | | * @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(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件的MD5 |
| | | * @param filePath |
| | | * @return |
| | | * 1.获取文件的MD5 |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public static String getFileMd5(String filePath) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 字节码转16进制 |
| | | */ |
| | | public static String byteToHexString(byte[] tmp) { |
| | | // 每个字节用 16 进制表示的话,使用两个字符, |
| | | char[] str = new char[16 * 2]; |
| | |
| | | // 换后的结果转换为字符串 |
| | | 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<File> 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); |
| | | } |
| | | } |
| | | } else { |
| | | list.add(file); |
| | | } |
| | | } |
| | | } |