底下的作法,在讀取檔案的同時,進行MD5計算, 省時又方便!
public String getMD5(InputStream is) {
try { byte[] buffer = new byte[1024]; MessageDigest mdf = MessageDigest.getInstance("MD5"); int num; do { num = is.read(buffer); if(num > 0){ mdf.update(buffer, 0, num); //同時可以處理讀進來的buffer //....... } } while(num != -1); is.close(); return toHexString(mdf.digest()); } catch (Exception e) { } return null; }private String toHexString(byte[] bytes) {StringBuilder hexString = new StringBuilder(); for (byte b : bytes) hexString.append(Integer.toHexString(0xFF & b)); return hexString.toString(); }
沒有留言:
張貼留言