From 2d2d9138fbe5c9c43f070be59a2d10f18fbf4c3b Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 23 十一月 2022 09:05:06 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/FileHelper.java | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 105 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java index 920f250..85bfbd4 100644 --- a/src/main/java/com/lf/server/helper/FileHelper.java +++ b/src/main/java/com/lf/server/helper/FileHelper.java @@ -1,11 +1,15 @@ package com.lf.server.helper; +import com.lf.server.entity.data.MetaFileEntity; 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.text.DecimalFormat; +import java.util.List; /** * 鏂囦欢甯姪绫� @@ -17,6 +21,23 @@ public static final double D1024 = 1024.0; public static final double D1050 = 1050.0; + + private final static Log log = LogFactory.getLog(FileHelper.class); + + /** + * 鑾峰彇鏂囦欢鍚� + * + * @param file + * @return + */ + public static String getFileName(String file) { + int idx = file.lastIndexOf(File.separator); + if (idx > -1) { + return file.substring(idx + 1); + } + + return ""; + } /** * 鑾峰彇鏂囦欢鎵╁睍鍚� @@ -186,4 +207,88 @@ return md5; } + + /** + * 鍒犻櫎鏂囦欢 + */ + public static void deleteFiles(List<MetaFileEntity> list) { + try { + for (MetaFileEntity mf : list) { + File f = new File(mf.getPath()); + if (f.exists()) { + f.delete(); + } + } + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + + /** + * 鍒犻櫎鏂囦欢澶� + * + * @param dir 鏂囦欢澶� + */ + public static void deleteDir(String dir) { + File file = new File(dir); + + deleteFiles(file); + } + + /** + * 绾ц仈鍒犻櫎鏂囦欢 + * + * @param file 鏂囦欢 + */ + public static void deleteFiles(File file) { + if (file == null || !file.exists()) { + return; + } + + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (File f : files) { + if (f.isDirectory()) { + deleteFiles(f); + } else { + f.delete(); + } + } + } + + file.delete(); + } + + /** + * 鑾峰彇鐩稿璺緞 + * + * @param file 鏂囦欢 + * @return 鐩稿璺緞 + */ + public static String getRelativePath(String file) { + if (StringHelper.isEmpty(file)) { + return null; + } + + int idx = file.lastIndexOf(File.separator); + int start = file.lastIndexOf(File.separator, idx - 1); + + return file.substring(start + 1); + } + + /** + * 鑾峰彇璺緞 + * + * @param file 鏂囦欢 + * @return 鏂囦欢璺緞 + */ + public static String getPath(String file) { + if (StringHelper.isEmpty(file)) { + return null; + } + + int end = file.lastIndexOf(File.separator); + + return file.substring(0, end); + } } -- Gitblit v1.9.3