|
- public static String getMD5(File file)
# p$ m0 H7 }2 G8 g4 s - {
9 e8 _# H0 L' c& H - String md5 = "";" s5 {: e$ M% E
- try " n" T N: i' _9 g7 T5 a
- {
/ j1 A7 l. A" \4 _ G9 X H - FileInputStream fis = new FileInputStream(file);" `# t( D9 j" r
- MessageDigest md = MessageDigest.getInstance("MD5");
/ b% ]: G4 `, w+ ~2 X) X( N - byte[] buffer = new byte[1024];
2 ~. m8 \" ]/ {2 @ - int length = -1;( f7 ]3 z& V5 E0 Y' U5 I5 ]0 p
- while ((length = fis.read(buffer, 0, 1024)) != -1) g J& @2 [% S& p
- {
* M& \# {: t" W9 O - md.update(buffer, 0, length);4 |. f% k2 x5 X4 G4 Q) K7 v; s
- }
- ? z8 p& y) {# L4 N$ S - BigInteger bigInt = new BigInteger(1, md.digest());& Y4 ~& P: @: v( v
- md5 = bigInt.toString(16);- _2 n1 h' k4 _4 }
- } catch (FileNotFoundException e) {; t. l1 ]* \ D! i7 k
- e.printStackTrace();2 N$ C, W5 G7 i" R; t, L; B
- } catch (NoSuchAlgorithmException e) {' l! {) H' \$ q: b( j
- e.printStackTrace();# V1 D- w) x; q6 ~. V6 u
- } catch (IOException e) {# u+ K! O* s* b, W8 t
- e.printStackTrace();6 U- Y% O9 M/ ?- a6 ]/ ]; h( c# l# D
- }
- r+ v+ U; ^3 \: W - return md5;% t# F& c# _0 `5 ?( w
- }
5 u# ~% L- t2 H2 E- f/ `) C( A
复制代码 |
|