在网上找了很多根据邮箱获取Gravatar头像的教程都是php的...
好吧我也记录下来....
首先嘛,是需要到https://en.gravatar.com/ 注册或者登录吧..
然后可以根据php的语言转换...
php获取Gravatar头像是这样仔转换的
$email = "admin@199604.com"; $gravatarImg = 'https://s.gravatar.com/avatar/' . md5($email) . '?s=32';
因为我的邮箱转化为md5后是162d8d081d380691c5279df2bbef4152
所以这时候$gravatarImg 是https://s.gravatar.com/avatar/162d8d081d380691c5279df2bbef4152?s=32
其实?s=xx是图片大小
最后http://www.gravatar.com/avatar/162d8d081d380691c5279df2bbef4152?s=32 也是可以获取图片...
现在我们来对比一下官网的吧...
看到这个图片是不是和我上面解释相差无几吧..但是最重要的一点是...你的邮箱在Gravatar上存在上传的图片..
下面记录一下java实现吧..
package com.blog.util; import java.security.MessageDigest; public class Functions { public static String emailToMd5(String message){ String temp = ""; try{ MessageDigest md5Digest = MessageDigest.getInstance("MD5"); byte[] encodeMd5Digest = md5Digest.digest(message.getBytes()); temp = convertByteToHexString(encodeMd5Digest); }catch (Exception e) { e.printStackTrace(); } return temp; } public static String convertByteToHexString(byte[] bytes){ String result = ""; for(int i=0;i<bytes.length;i++){ int temp = bytes[i]&0xff; String tempHex = Integer.toHexString(temp); if(tempHex.length()<2){ result +="0"+tempHex; }else{ result +=tempHex; } } return result; } //根据email获取gravatar头像 public static String getGravatar(String email) { String emailMd5 = emailToMd5(email); //设置图片大小32px String avatar = "https://s.gravatar.com/avatar/"+emailMd5+"?s=32"; return avatar; } public static void main(String[] args) { // String emailMd5 = emailToMd5("admin@199604.com"); // System.out.println(emailMd5);//162d8d081d380691c5279df2bbef4152 // System.out.println(emailMd5.equals("162d8d081d380691c5279df2bbef4152"));//true String gravatarImg = getGravatar("admin@199604.com"); System.out.println(gravatarImg); //输出https://s.gravatar.com/avatar/162d8d081d380691c5279df2bbef4152?s=32 } }
原理其实很简单,就是把邮箱转化成md5,然后拼接到Gravatar图片地址上面..
如果你觉得不信的话,可以去实现....