Java tutorial
//package com.java2s; import android.text.TextUtils; import android.util.Log; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final String TAG = "WXUtil"; private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static final Pattern sPattern = Pattern.compile(".*filename=(.+?)&.*thumbnail=(.+?)&"); public static String getMD5FileName(String filePathName) { if (!TextUtils.isEmpty(filePathName)) { Matcher matcher = sPattern.matcher(filePathName); if (matcher.find()) { return matcher.group(1) + matcher.group(2); } } return getMD5Value(filePathName); } public static boolean isEmpty(String str) { if (str == null) { return true; } for (int i = 0, length = str.length(); i < length; i++) { if (!Character.isWhitespace(str.charAt(i))) { return false; } } return true; } public static String getMD5Value(String str, String fix) { return getMD5Value(str, fix, "utf-8"); } public static String getMD5Value(String str, String fix, String charsetName) { if (str != null && fix != null) { String formalString = str + fix; try { MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); try { algorithm.update(formalString.getBytes(charsetName)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block Log.e("WxException", e.getMessage(), e); return null; } byte messageDigest[] = algorithm.digest(); return toHexString(messageDigest); } catch (NoSuchAlgorithmException e) { Log.w(TAG, e); Log.e("WxException", e.getMessage(), e); } } return null; } public static String getMD5Value(String str) { return getMD5Value(str, ""); } /** * @hide * @param b * @return */ public static String toHexString(byte[] b) { // String to byte StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]); sb.append(HEX_DIGITS[b[i] & 0x0f]); } return sb.toString(); } }