| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | 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.nio.ByteBuffer; |
| | | import java.nio.channels.FileChannel; |
| | | import java.security.MessageDigest; |
| | | import java.text.DecimalFormat; |
| | | import java.util.List; |
| | | |
| | |
| | | * @author WWW |
| | | */ |
| | | public class FileHelper { |
| | | private final static String POINT = "."; |
| | | |
| | | public static final double D1024 = 1024.0; |
| | | |
| | | public static final double D1050 = 1050.0; |
| | | |
| | | private final static Log log = LogFactory.getLog(FileHelper.class); |
| | | |
| | | /** |
| | |
| | | |
| | | String fileName = file.getName().toLowerCase(); |
| | | |
| | | int idx = fileName.lastIndexOf(POINT); |
| | | int idx = fileName.lastIndexOf(StaticData.POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | |
| | | return ""; |
| | | } |
| | | |
| | | int idx = fileName.lastIndexOf(POINT); |
| | | int idx = fileName.lastIndexOf(StaticData.POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | |
| | | * 字节单位换算 |
| | | */ |
| | | public static String formatByte(long byteNumber) { |
| | | double kbNumber = byteNumber / D1024; |
| | | if (kbNumber < D1024) { |
| | | double kbNumber = byteNumber / StaticData.D1024; |
| | | if (kbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##KB").format(kbNumber); |
| | | } |
| | | double mbNumber = kbNumber / D1024; |
| | | if (mbNumber < D1024) { |
| | | double mbNumber = kbNumber / StaticData.D1024; |
| | | if (mbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##MB").format(mbNumber); |
| | | } |
| | | double gbNumber = mbNumber / D1024; |
| | | if (gbNumber < D1024) { |
| | | double gbNumber = mbNumber / StaticData.D1024; |
| | | if (gbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##GB").format(gbNumber); |
| | | } |
| | | double tbNumber = gbNumber / D1024; |
| | | double tbNumber = gbNumber / StaticData.D1024; |
| | | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | | if (size < D1050) { |
| | | if (size < StaticData.D1050) { |
| | | return 0.001; |
| | | } |
| | | |
| | | String str = String.format("%.3f", size / D1024 / D1024); |
| | | String str = String.format("%.3f", size / StaticData.D1024 / StaticData.D1024); |
| | | |
| | | return Double.parseDouble(str); |
| | | } |
| | |
| | | fileStream.close(); |
| | | |
| | | return md5; |
| | | } |
| | | |
| | | /** |
| | | * 获取快速 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | return file.substring(0, end); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件的MD5 |
| | | * @param file |
| | | * @return |
| | | */ |
| | | @SuppressWarnings("unused") |
| | | public static String getFileMd5(File file) { |
| | | FileInputStream fis = null; |
| | | try { |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | |
| | | fis = new FileInputStream(file); |
| | | 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(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static String byteToHexString(byte[] tmp) { |
| | | // 每个字节用 16 进制表示的话,使用两个字符, |
| | | char[] str = new char[16 * 2]; |
| | | |
| | | // 所以表示成 16 进制需要 32 个字符,表示转换结果中对应的字符位置 |
| | | int k = 0; |
| | | // 从第一个字节开始,对 MD5 的每一个字节 |
| | | for (int i = 0; i < StaticData.SIXTEEN; i++) { |
| | | // 转换成 16 进制字符的转换 |
| | | byte byte0 = tmp[i]; |
| | | // 取字节中高 4 位的数字转换 |
| | | str[k++] = StaticData.HEX_DIGITS[byte0 >>> 4 & 0xf]; |
| | | // >>> 为逻辑右移,将符号位一起右移, 取字节中低 4 位的数字转换 |
| | | str[k++] = StaticData.HEX_DIGITS[byte0 & 0xf]; |
| | | } |
| | | // 换后的结果转换为字符串 |
| | | return new String(str); |
| | | } |
| | | } |