有两种解决方案,一种是用MessageDigest来实现,另种是用org.apache.commons.codec.digest来实现,下面是代码:
package com.creditease.mobile.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import org.apache.commons.codec.digest.*;
import org.apache.commons.io.IOUtils;
public class MD5Util {
/**
* 用MessageDigest获取文件的md5值
* @param file
* @return
* @throws FileNotFoundException
*/
public static String getMd5ByMessageDigest(String filePath) throws FileNotFoundException {
File file = new File(filePath);
String value = null;
FileInputStream in = new FileInputStream(file);
try {
MappedByteBuffer byteBuffer = in.getChannel().map(
FileChannel.MapMode.READ_ONLY, 0, file.length());
M