|
- public static String getMD5(File file)1 |0 M& s$ W2 ?
- {
% n' \0 U/ L% z4 f, { - String md5 = "";
2 `' o7 A) N1 w q: U7 c2 k - try * a; B' M2 ]1 U* g* \2 @
- {4 q9 p: X4 m; Y. G# V
- FileInputStream fis = new FileInputStream(file);
2 W+ Q5 J2 J! D! y2 x U - MessageDigest md = MessageDigest.getInstance("MD5");& V3 G: f6 s+ q( d
- byte[] buffer = new byte[1024];
) m( C1 D( \& I: ^" k/ x - int length = -1;& f* M' t: k- D) f& j0 C0 U7 M
- while ((length = fis.read(buffer, 0, 1024)) != -1)4 C H2 O& Z7 c# t0 p( z
- {3 l Z1 w" d, v: q
- md.update(buffer, 0, length);
, b9 r8 t. l; Q$ q. ^: g. H - }
; j8 M; q" G% A5 Q) \2 ` - BigInteger bigInt = new BigInteger(1, md.digest());1 f; [" `$ I' K) W; q V
- md5 = bigInt.toString(16);
; S4 s1 p8 D6 Y/ s/ I' A - } catch (FileNotFoundException e) {; i5 q: N( c( J) O( _
- e.printStackTrace();
+ z& t3 p3 b. G9 N: N - } catch (NoSuchAlgorithmException e) {
; j+ ]8 T- f! H - e.printStackTrace();* \! E- h* Y; e1 f" |# m% t
- } catch (IOException e) {
& b. D* m, r' g$ } - e.printStackTrace();
5 K( M. V1 f9 y! j/ R* w - }, a. j" n9 {2 c/ h# A
- return md5;6 j# W% o3 p) C+ x
- }
& F! X, ?, m4 N" a& Y3 A; `$ L
复制代码 |
|