文件MD5

*根据文件获取MD5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//获取单个文件的MD5的值
public static String mD5(MultipartFile file)
{
if (file == null && StringUtil.isNull(file.getOriginalFilename()))
{
return null;
}
MessageDigest digest = null;
InputStream fis = null;
byte buffer[] = new byte[1024];
int len;
try
{
digest = MessageDigest.getInstance("MD5");
fis = file.getInputStream();
while ((len = fis.read(buffer, 0, 1024)) != -1)
{
digest.update(buffer, 0, len);
}
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return bytesToHexString(digest.digest());
}

private static String bytesToHexString(byte[] src)
{
if (src == null || src.length <= 0)
{
return null;
}
StringBuilder stringBuilder = new StringBuilder();
for (byte aSrc : src)
{
int v = aSrc & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2)
{
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
Donate comment here