¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.simu.helper; |
| | | |
| | | import com.twmacinta.util.MD5; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | |
| | | 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; |
| | | |
| | | /** |
| | | * æä»¶å¸®å©ç±» |
| | | * |
| | | * @author WWW |
| | | * @date 2024-09-12 |
| | | */ |
| | | @Slf4j |
| | | public class FileHelper { |
| | | public final static String POINT = "."; |
| | | |
| | | public final static int I16 = 16; |
| | | |
| | | public static final double D1024 = 1024.0; |
| | | |
| | | public static final double D1050 = 1050.0; |
| | | |
| | | public final static int I1000000 = 1000000; |
| | | |
| | | public static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; |
| | | |
| | | /** |
| | | * è·åæä»¶å |
| | | */ |
| | | public static String getFileName(String file) { |
| | | int idx = file.lastIndexOf(File.separator); |
| | | if (idx > -1) { |
| | | return file.substring(idx + 1); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶æ©å±å |
| | | */ |
| | | public static String getExtension(File file) { |
| | | if (file == null) { |
| | | return null; |
| | | } |
| | | |
| | | String fileName = file.getName().toLowerCase(); |
| | | |
| | | int idx = fileName.indexOf(POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | | |
| | | return fileName.substring(idx); |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶æ©å±å |
| | | */ |
| | | public static String getExtension(String fileName) { |
| | | if (StringHelper.isEmpty(fileName)) { |
| | | return ""; |
| | | } |
| | | |
| | | int idx = fileName.lastIndexOf(POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | | |
| | | return fileName.substring(idx).toLowerCase(); |
| | | } |
| | | |
| | | /** |
| | | * è·åå¤ç¨éäºèç½é®ä»¶æ©å±ç±»å |
| | | */ |
| | | public static String getMime(String ext) { |
| | | switch (ext) { |
| | | // å¾ç |
| | | case ".tif": |
| | | case ".tiff": |
| | | return "image/tiff"; |
| | | case ".img": |
| | | return "application/x-img"; |
| | | case ".gif": |
| | | return "image/gif"; |
| | | case ".jpg": |
| | | case ".jpeg": |
| | | return "image/jpeg"; |
| | | case ".png": |
| | | return "image/png"; |
| | | // é³/è§é¢ |
| | | case ".mp3": |
| | | return "audio/mp3"; |
| | | case ".mp4": |
| | | return "video/mpeg4"; |
| | | case ".avi": |
| | | return "video/avi"; |
| | | case ".mpg": |
| | | case ".mpeg": |
| | | return "video/mpg"; |
| | | case ".wav": |
| | | return "audio/wav"; |
| | | case ".wma": |
| | | return "audio/x-ms-wma"; |
| | | case ".swf": |
| | | return "application/x-shockwave-flash"; |
| | | case ".wmv": |
| | | return "video/x-ms-wmv"; |
| | | case ".rm": |
| | | return "application/vnd.rn-realmedia"; |
| | | case ".rmvb": |
| | | return "application/vnd.rn-realmedia-vbr"; |
| | | // ç½é¡µ |
| | | case ".js": |
| | | return "application/x-javascript"; |
| | | case ".css": |
| | | return "text/css"; |
| | | case ".asp": |
| | | return "text/asp"; |
| | | case ".mht": |
| | | return "message/rfc822"; |
| | | case ".jsp": |
| | | case ".htm": |
| | | case ".html": |
| | | case ".xhtml": |
| | | return "text/html"; |
| | | case ".xml": |
| | | case ".svg": |
| | | return "text/xml"; |
| | | // æä»¶ |
| | | case ".txt": |
| | | return "text/plain"; |
| | | case ".dbf": |
| | | return "application/x-dbf"; |
| | | case ".mdb": |
| | | return "application/msaccess"; |
| | | case ".pdf": |
| | | return "application/pdf"; |
| | | case ".ppt": |
| | | case ".pptx": |
| | | return "application/x-ppt"; |
| | | case ".doc": |
| | | case ".docx": |
| | | return "application/msword"; |
| | | case ".xls": |
| | | case ".xlsx": |
| | | return "application/vnd.ms-excel"; |
| | | case ".dgn": |
| | | return "application/x-dgn"; |
| | | case ".dwg": |
| | | return "application/x-dwg"; |
| | | case ".ext": |
| | | return "application/x-msdownload"; |
| | | // é»è®¤ |
| | | default: |
| | | return "application/octet-stream"; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åèå使¢ç® |
| | | */ |
| | | public static String formatByte(long byteNumber) { |
| | | double kbNumber = byteNumber / D1024; |
| | | if (kbNumber < D1024) { |
| | | return new DecimalFormat("#.##KB").format(kbNumber); |
| | | } |
| | | double mbNumber = kbNumber / D1024; |
| | | if (mbNumber < D1024) { |
| | | return new DecimalFormat("#.##MB").format(mbNumber); |
| | | } |
| | | double gbNumber = mbNumber / D1024; |
| | | if (gbNumber < D1024) { |
| | | return new DecimalFormat("#.##GB").format(gbNumber); |
| | | } |
| | | double tbNumber = gbNumber / D1024; |
| | | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | | |
| | | /** |
| | | * è·åå¹³æ¹ç±³ |
| | | */ |
| | | public static String getSquareMeter(double num) { |
| | | if (num < I1000000) { |
| | | return new DecimalFormat("#.##å¹³æ¹ç±³").format(num); |
| | | } |
| | | |
| | | double knum = num / I1000000; |
| | | |
| | | return new DecimalFormat("#.##å¹³æ¹åç±³").format(knum); |
| | | } |
| | | |
| | | /** |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | | if (size < D1050) { |
| | | return 0.001; |
| | | } |
| | | |
| | | String str = String.format("%.3f", size / D1024 / D1024); |
| | | |
| | | return Double.parseDouble(str); |
| | | } |
| | | |
| | | /** |
| | | * 3.è·åæä»¶MD5ç ï¼JDKï¼ |
| | | */ |
| | | public static String getMd5ByJdk(String filePath) throws IOException { |
| | | FileInputStream fileStream = new FileInputStream(filePath); |
| | | String md5 = DigestUtils.md5Hex(fileStream); |
| | | fileStream.close(); |
| | | |
| | | return md5; |
| | | } |
| | | |
| | | /** |
| | | * 2.è·åå¿«é MD5 ç |
| | | */ |
| | | public static String getFastMd5(String filePath) throws IOException { |
| | | String hash = MD5.asHex(MD5.getHash(new File(filePath))); |
| | | |
| | | MD5 md5 = new MD5(); |
| | | md5.Update(hash, null); |
| | | |
| | | return md5.asHex(); |
| | | } |
| | | |
| | | /** |
| | | * å 餿件夹 |
| | | * |
| | | * @param dir æä»¶å¤¹ |
| | | */ |
| | | public static void deleteDir(String dir) { |
| | | File file = new File(dir); |
| | | |
| | | deleteFiles(file); |
| | | } |
| | | |
| | | /** |
| | | * 级èå 餿件 |
| | | * |
| | | * @param file æä»¶ |
| | | */ |
| | | public static void deleteFiles(File file) { |
| | | if (null == file || !file.exists()) { |
| | | return; |
| | | } |
| | | |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | if (null != files && files.length > 0) { |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * 1.è·åæä»¶çMD5 |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public static String getFileMd5(String filePath) { |
| | | FileInputStream fis = null; |
| | | try { |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | |
| | | fis = new FileInputStream(new File(filePath)); |
| | | FileChannel fChannel = fis.getChannel(); |
| | | ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 1024); |
| | | |
| | | while (fChannel.read(buffer) != -1) { |
| | | buffer.flip(); |
| | | md.update(buffer); |
| | | buffer.compact(); |
| | | } |
| | | byte[] b = md.digest(); |
| | | |
| | | return byteToHexString(b); |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | return null; |
| | | } finally { |
| | | try { |
| | | if (null != fis) { |
| | | fis.close(); |
| | | } |
| | | } catch (IOException ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * åèç 转16è¿å¶ |
| | | */ |
| | | public static String byteToHexString(byte[] tmp) { |
| | | // æ¯ä¸ªåèç¨ 16 è¿å¶è¡¨ç¤ºçè¯ï¼ä½¿ç¨ä¸¤ä¸ªåç¬¦ï¼ |
| | | char[] str = new char[16 * 2]; |
| | | |
| | | // æä»¥è¡¨ç¤ºæ 16 è¿å¶éè¦ 32 个å符ï¼è¡¨ç¤ºè½¬æ¢ç»æä¸å¯¹åºçå符ä½ç½® |
| | | int k = 0; |
| | | // ä»ç¬¬ä¸ä¸ªåèå¼å§ï¼å¯¹ MD5 çæ¯ä¸ä¸ªåè |
| | | for (int i = 0; i < I16; i++) { |
| | | // è½¬æ¢æ 16 è¿å¶å符çè½¬æ¢ |
| | | byte byte0 = tmp[i]; |
| | | // ååèä¸é« 4 ä½çæ°åè½¬æ¢ |
| | | str[k++] = HEX_DIGITS[byte0 >>> 4 & 0xf]; |
| | | // >>> 为é»è¾å³ç§»ï¼å°ç¬¦å·ä½ä¸èµ·å³ç§»ï¼ ååèä¸ä½ 4 ä½çæ°åè½¬æ¢ |
| | | str[k++] = HEX_DIGITS[byte0 & 0xf]; |
| | | } |
| | | // æ¢åçç»æè½¬æ¢ä¸ºå符串 |
| | | 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(); |
| | | } |
| | | } |
| | | } |