代碼如下:
File file = new File("d:/1.dat");
FileInputStream fs = new FileInputStream(file);
BufferedInputStream bi = new BufferedInputStream(fs);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] b = new byte[bi.available()];
int i;
while ((i = bi.read(b, 0, b.length)) != -1)
{
bo.write(b, 0, i);
}
bo.close();
System.out.print(Md5.MD5(new String(bo.toByteArray())));
public final static String MD5(String s)
{
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try
{
byte[] strTemp = s.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++)
{
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
}
catch (Exception e)
{
return null;
}
}
Md5.MD5就是通過string天生md5編碼.
後來在網高低載了一個看文件的md5編碼的軟件,和我出來的成果不一樣.
查詢一些材料,似乎應當是讀文件簽名或者認證的md5,假如文件比擬大,也不可能把所有的文件數據讀到內存.
假如讀文件認證的md5或者文件hashcode的md5