首页 > 代码库 > URL短地址压缩算法
URL短地址压缩算法
时下,短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn,新郎的t.cn等。我们在新浪微博上发布网址的时候,微博会自动判别网址,并将其转换,例如:http://t.cn/hrYnr0。为什么要这样做的,原因我想有这样几点:
1、微博限制字数为140字一条,那么如果我们需要发一些连接上去,但是这个连接非常的长,以至于将近要占用我们内容的一半篇幅,这肯定是不能被允许的,所以短网址应运而生了。
2、短网址可以在我们项目里可以很好的对开放级URL进行管理。有一部分网址可以会涵盖性、暴力、广告等信息,这样我们可以通过用户的举报,完全管理这个连接将不出现在我们的应用中,应为同样的URL通过加密算法之后,得到的地址是一样的。
3、我们可以对一系列的网址进行流量,点击等统计,挖掘出大多数用户的关注点,这样有利于我们对项目的后续工作更好的作出决策。
以上三点纯属个人观点,因为在我接下来的部分项目中会应用到,所以就了解了一下,下面先来看看短网址映射算法的理论(网上找到的资料):
① 将长网址用md5算法生成32位签名串,分为4段,,每段8个字符;
② 对这4段循环处理,取每段的8个字符, 将他看成16进制字符串与0x3fffffff(30位1)的位与操作,超过30位的忽略处理;
③ 将每段得到的这30位又分成6段,每5位的数字作为字母表的索引取得特定字符,依次进行获得6位字符串;
④ 这样一个md5字符串可以获得4个6位串,取里面的任意一个就可作为这个长url的短url地址。
我们并不一定说得到的URL是唯一的,但是我们能够取出4组URL,这样几乎不会出现太大的重复。
首先,请大家了解在Java中如何用MD5来加密字符串得到32位的加密后的字符串,下面是我已经封装好的Java MD5算法:
【MD5encry.class】
package com.example.demo_shorturl; import java.security.MessageDigest; /** * ClassName:MD5encry <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2014-8-13 上午9:51:15 <br/> * @author Geek_AnJon * @version * @since JDK 1.6 * @see */ public class MD5encry { private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; public static String byteArrayToHexString(byte[] b){ StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++){ resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b){ int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } public static String MD5Encode(String origin){ String resultString = null; try { resultString=new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); resultString.trim(); resultString=byteArrayToHexString(md.digest(resultString.getBytes("UTF-8"))); }catch (Exception ex){} return resultString; } public static void main(String[] args) { String data = http://www.mamicode.com/"189022881112011111118:09sz0000123456789987654321";>【ShortUrl.class】package com.example.demo_shorturl; /** * ClassName:ShortUrl <br/> * Function: TODO ADD FUNCTION. <br/> * Reason: TODO ADD REASON. <br/> * Date: 2014-8-13 上午9:48:34 <br/> * @author Geek_AnJon * @version * @since JDK 1.6 * @see */ public class ShortUrl { public static void main(String[] args) { String url = "http://www.baidu.com"; for (String string : ShortText(url)) { print(string); } } public static String[] ShortText(String string){ String key = "Geek"; //自定义生成MD5加密字符串前的混合KEY String[] chars = new String[]{ //要使用生成URL的字符 "a","b","c","d","e","f","g","h", "i","j","k","l","m","n","o","p", "q","r","s","t","u","v","w","x", "y","z","0","1","2","3","4","5", "6","7","8","9","A","B","C","D", "E","F","G","H","I","J","K","L", "M","N","O","P","Q","R","S","T", "U","V","W","X","Y","Z" }; String hex = MD5encry.MD5Encode(key + string); int hexLen = hex.length(); int subHexLen = hexLen / 8; String[] ShortStr = new String[4]; for (int i = 0; i < subHexLen; i++) { String outChars = ""; int j = i + 1; String subHex = hex.substring(i * 8, j * 8); long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16); for (int k = 0; k < 6; k++) { int index = (int) (Long.valueOf("0000003D", 16) & idx); outChars += chars[index]; idx = idx >> 5; } ShortStr[i] = outChars; } return ShortStr; } private static void print(Object messagr){ System.out.println(messagr); } }
现在可以直接使用ShortText(url)方法获取短链接地址了