From 71726a1cb05790d6841595ef7daef5173f2cddfa Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 29 三月 2023 09:43:11 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/FileHelper.java | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 103 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java index b21e617..5c3f682 100644 --- a/src/main/java/com/lf/server/helper/FileHelper.java +++ b/src/main/java/com/lf/server/helper/FileHelper.java @@ -1,21 +1,20 @@ 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 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; +import java.util.Objects; /** * 鏂囦欢甯姪绫� @@ -49,7 +48,7 @@ String fileName = file.getName().toLowerCase(); - int idx = fileName.lastIndexOf(StaticData.POINT); + int idx = fileName.indexOf(StaticData.POINT); if (idx == -1) { return ""; } @@ -185,6 +184,24 @@ } /** + * 鑾峰彇鏂囦欢澶у皬 + */ + 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杞琈B */ public static double sizeToMb(long size) { @@ -198,7 +215,7 @@ } /** - * 鑾峰彇鏂囦欢MD5鐮侊紙JDK锛� + * 3.鑾峰彇鏂囦欢MD5鐮侊紙JDK锛� */ public static String getMd5ByJdk(String filePath) throws IOException { FileInputStream fileStream = new FileInputStream(filePath); @@ -209,7 +226,7 @@ } /** - * 鑾峰彇蹇�� MD5 鐮� + * 2.鑾峰彇蹇�� MD5 鐮� */ public static String getFastMd5(String filePath) throws IOException { String hash = MD5.asHex(MD5.getHash(new File(filePath))); @@ -253,17 +270,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(); + } } } } @@ -305,9 +324,7 @@ } /** - * 鑾峰彇鏂囦欢鐨凪D5 - * @param filePath - * @return + * 1.鑾峰彇鏂囦欢鐨凪D5 */ @SuppressWarnings("unused") public static String getFileMd5(String filePath) { @@ -341,6 +358,9 @@ } } + /** + * 瀛楄妭鐮佽浆16杩涘埗 + */ public static String byteToHexString(byte[] tmp) { // 姣忎釜瀛楄妭鐢� 16 杩涘埗琛ㄧず鐨勮瘽锛屼娇鐢ㄤ袱涓瓧绗︼紝 char[] str = new char[16 * 2]; @@ -359,4 +379,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<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); + } + } + + /** + * 澶嶅埗鏂囦欢 + * + * @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(); + } + } } -- Gitblit v1.9.3