目录

Gravatar-Globally Recognized Avatar的缩写,也就是 http://www.gravatar.com 推出的一项服务,意为“全球通用头像”。如果在Gravatar的服务器上放置了你自己的头像,那么在任何支持Gravatar的blog或者留言本上留言时,只要提供你与这个头像关联的email地址,就能够显示出你的Gravatar头像来!但是现在国内被墙了,这里就利用多说的镜像来获取。


Gravatar api使用非常方便,这里有官方的示例,就是获取邮件地址的哈希值:

import java.util.*;
import java.io.*;
import java.security.*;
public class MD5Util {
  public static String hex(byte[] array) {
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < array.length; ++i) {
      sb.append(Integer.toHexString((array[i]
          & 0xFF) | 0x100).substring(1,3));        
      }
      return sb.toString();
  }
  public static String md5Hex (String message) {
      try {
      MessageDigest md = 
          MessageDigest.getInstance("MD5");
      return hex (md.digest(message.getBytes("CP1252")));
      } catch (NoSuchAlgorithmException e) {
      } catch (UnsupportedEncodingException e) {
      }
      return null;
  }
}

String email = "someone@somewhere.com";
String hash = "http://www.gravatar.com/" + MD5Util.md5Hex(email);

而我们这里使用多说的镜像,就该这样:

String email = "806916924@qq.com";
String uimg = "http://gravatar.duoshuo.com/avatar/" + MD5Util.md5Hex(email);

不出意外获取出来uimg就是:

http://gravatar.duoshuo.com/avatar/ed6a669dff79932f57b3720f396274ba