From 5573ab68a6bf98014e6948d0c5ab538a90ce0ada Mon Sep 17 00:00:00 2001 From: sws <15810472099@163.com> Date: 星期六, 14 一月 2023 09:46:15 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/FileHelper.java | 69 +++++++++++++++++++++++++++++----- 1 files changed, 58 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java index c9d15ee..1f19c9a 100644 --- a/src/main/java/com/lf/server/helper/FileHelper.java +++ b/src/main/java/com/lf/server/helper/FileHelper.java @@ -12,9 +12,11 @@ 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; /** * 鏂囦欢甯姪绫� @@ -197,7 +199,7 @@ } /** - * 鑾峰彇鏂囦欢MD5鐮侊紙JDK锛� + * 3.鑾峰彇鏂囦欢MD5鐮侊紙JDK锛� */ public static String getMd5ByJdk(String filePath) throws IOException { FileInputStream fileStream = new FileInputStream(filePath); @@ -208,7 +210,7 @@ } /** - * 鑾峰彇蹇�� MD5 鐮� + * 2.鑾峰彇蹇�� MD5 鐮� */ public static String getFastMd5(String filePath) throws IOException { String hash = MD5.asHex(MD5.getHash(new File(filePath))); @@ -252,17 +254,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,9 +308,7 @@ } /** - * 鑾峰彇鏂囦欢鐨凪D5 - * @param filePath - * @return + * 1.鑾峰彇鏂囦欢鐨凪D5 */ @SuppressWarnings("unused") public static String getFileMd5(String filePath) { @@ -340,6 +342,9 @@ } } + /** + * 瀛楄妭鐮佽浆16杩涘埗 + */ public static String byteToHexString(byte[] tmp) { // 姣忎釜瀛楄妭鐢� 16 杩涘埗琛ㄧず鐨勮瘽锛屼娇鐢ㄤ袱涓瓧绗︼紝 char[] str = new char[16 * 2]; @@ -358,4 +363,46 @@ // 鎹㈠悗鐨勭粨鏋滆浆鎹负瀛楃涓� 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); + } + } } -- Gitblit v1.9.3