1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package com.fdkankan.fusion.common.util;
- import java.io.File;
- import java.io.FileInputStream;
- import java.security.MessageDigest;
- public class FileMd5Util {
- public static final String KEY_MD5 = "MD5";
- public static final String CHARSET_ISO88591 = "ISO-8859-1";
- public FileMd5Util() {
- }
- public static String getFileMD5(File file) {
- if (file.exists() && file.isFile()) {
- MessageDigest digest = null;
- FileInputStream in = null;
- byte[] buffer = new byte[1024];
- try {
- digest = MessageDigest.getInstance("MD5");
- in = new FileInputStream(file);
- while(true) {
- int len;
- if ((len = in.read(buffer, 0, 1024)) == -1) {
- in.close();
- break;
- }
- digest.update(buffer, 0, len);
- }
- } catch (Exception var9) {
- var9.printStackTrace();
- return null;
- }
- byte[] by = digest.digest();
- StringBuffer sbf = new StringBuffer();
- for(int j = 0; j < by.length; ++j) {
- int i = by[j];
- if (i < 0) {
- i += 256;
- } else if (i < 16) {
- sbf.append("0");
- }
- sbf.append(Integer.toHexString(i));
- }
- return sbf.toString();
- } else {
- return null;
- }
- }
- public static String getFileMD5(String filepath) {
- File file = new File(filepath);
- return getFileMD5(file);
- }
- public static byte[] encryptMD5(byte[] data) throws Exception {
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- md5.update(data);
- return md5.digest();
- }
- public static byte[] encryptMD5(String data) throws Exception {
- return encryptMD5(data.getBytes("ISO-8859-1"));
- }
- public static boolean isSameMd5(File file1, File file2) {
- String md5_1 = getFileMD5(file1);
- String md5_2 = getFileMD5(file2);
- return md5_1.equals(md5_2);
- }
- public static boolean isSameMd5(String filepath1, String filepath2) {
- File file1 = new File(filepath1);
- File file2 = new File(filepath2);
- return isSameMd5(file1, file2);
- }
- public static void main(String[] args) {
- System.out.println("obj1: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q-copy.obj")));
- System.out.println("obj: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q.obj")));
- }
- }
|