|
|
@@ -0,0 +1,136 @@
|
|
|
+package com.fdkankan.fyun.util;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.security.MessageDigest;
|
|
|
+
|
|
|
+public class MD5Checksum {
|
|
|
+ public static byte[] createChecksum(String filePath,String sign) {
|
|
|
+ try {
|
|
|
+ InputStream fis = new FileInputStream(filePath);
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ MessageDigest complete = MessageDigest.getInstance(sign);
|
|
|
+ int numRead;
|
|
|
+
|
|
|
+ do {
|
|
|
+ numRead = fis.read(buffer);
|
|
|
+ if (numRead > 0) {
|
|
|
+ complete.update(buffer, 0, numRead);
|
|
|
+ }
|
|
|
+ } while (numRead != -1);
|
|
|
+
|
|
|
+ fis.close();
|
|
|
+ return complete.digest();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static byte[] createChecksum(InputStream fis,String sign) {
|
|
|
+ try {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ MessageDigest complete = MessageDigest.getInstance(sign);
|
|
|
+ int numRead;
|
|
|
+
|
|
|
+ do {
|
|
|
+ numRead = fis.read(buffer);
|
|
|
+ if (numRead > 0) {
|
|
|
+ complete.update(buffer, 0, numRead);
|
|
|
+ }
|
|
|
+ } while (numRead != -1);
|
|
|
+
|
|
|
+ return complete.digest();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMD5(String filePath) {
|
|
|
+ byte[] b = createChecksum(filePath,"MD5");
|
|
|
+ if(b == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+
|
|
|
+ for (byte element : b) {
|
|
|
+ result.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getMD5(InputStream filePath) {
|
|
|
+ byte[] b = createChecksum(filePath,"MD5");
|
|
|
+ if(b == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+
|
|
|
+ for (byte element : b) {
|
|
|
+ result.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSHA1(String filePath) {
|
|
|
+ byte[] b = createChecksum(filePath,"SHA1");
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ if(b == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (byte element : b) {
|
|
|
+ result.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ public static String getSHA1(InputStream filePath) {
|
|
|
+ byte[] b = createChecksum(filePath,"SHA1");
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ if(b == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (byte element : b) {
|
|
|
+ result.append(Integer.toString((element & 0xff) + 0x100, 16).substring(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Long getLastModifiedTime(String filePath) {
|
|
|
+ File file = new File(filePath);
|
|
|
+ return file.lastModified();
|
|
|
+ }
|
|
|
+ public static Long getSize(String filePath) {
|
|
|
+ File file = new File(filePath);
|
|
|
+ return file.length();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static FileInfoVo getFileInfo(String filePath){
|
|
|
+ try {
|
|
|
+ return new FileInfoVo(
|
|
|
+ getMD5(filePath).toUpperCase(),
|
|
|
+ getSHA1(filePath).toUpperCase(),
|
|
|
+ getLastModifiedTime(filePath),
|
|
|
+ getSize(filePath));
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ FileInfoVo fileInfo = getFileInfo("D:\\abc\\1.txt");
|
|
|
+ FileUtil.writeString(fileInfo.toString(),new File("D:\\abc\\hash.txt"),"UTF-8");
|
|
|
+ System.out.println();
|
|
|
+ }
|
|
|
+}
|