微博上經常會看到類似 http://t.cn/Afafhe 這樣的短地址
那么實現原理是什么呢
其實很簡單 ,系統把一個長的地址 如 http://www.xxx.com/ddd/xxx/a.html?dsada
首先用一個算法轉換成 短地址 http://t.cn/Afafhe
然后把 Afafhe-->http://www.xxx.com/ddd/xxx/a.html?dsada 的關系保存到數據庫中
當用戶訪問 http://t.cn/Afafhe網址時,系統到數據庫找到對應的URL地址,實現跳轉
那么我們要知道的1、算法 2、系統的存儲方式
首先看算法吧,網上搜索了下,大致是用MD5什么的生成的 ,其實這個算法主要是把長字符串變小 ,這個算法是不可逆的,所以別想着去直接反轉短地址
要詳細看算法的 可以到網上搜索資料
2、系統的存儲方式 ,如果我們自己寫着玩,那直接找個SQL Server 或者MySql 之類的就可以,但是想新浪微博之類的大型網站,那個數據量是非常巨大的,我想他們應該用的NoSql 非關系型數據庫(應該也就是人們說的分布式數據庫 ),一些開源的 如Facebook 的Cassandra, Apache 的HBase,也得到了廣泛認同。從這些NoSQL項目的名字上看不出什么相同之處:Hadoop、Voldemort、Dynomite,還有其它很多。、
http://baike.baidu.com/view/2677528.htm
Java的短地址算法
- package work13;
- import java.security.MessageDigest;
- public class CMyEncrypt
- {
- // 十六進制下數字到字符的映射數組
- private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D",
- "E", "F" };
- /** 把inputString加密 */
- public static String md5(String inputStr)
- {
- return encodeByMD5(inputStr);
- }
- /**
- * 驗證輸入的密碼是否正確
- *
- * @param password
- * 真正的密碼(加密后的真密碼)
- * @param inputString
- * 輸入的字符串
- * @return 驗證結果,boolean類型
- */
- public static boolean authenticatePassword(String password, String inputString)
- {
- if (password.equals(encodeByMD5(inputString)))
- {
- return true;
- } else
- {
- return false;
- }
- }
- /** 對字符串進行MD5編碼 */
- private static String encodeByMD5(String originString)
- {
- if (originString != null)
- {
- try
- {
- // 創建具有指定算法名稱的信息摘要
- MessageDigest md5 = MessageDigest.getInstance("MD5");
- // 使用指定的字節數組對摘要進行最后更新,然后完成摘要計算
- byte[] results = md5.digest(originString.getBytes());
- // 將得到的字節數組變成字符串返回
- String result = byteArrayToHexString(results);
- return result;
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- return null;
- }
- /**
- * 輪換字節數組為十六進制字符串
- *
- * @param b
- * 字節數組
- * @return 十六進制字符串
- */
- private 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 void main(String[] args)
- {
- CMyEncrypt.md5("http://tech.sina.com.cn/i/2011-03-23/11285321288.shtml");
- }
- }
- package work13;
- public class ShortUrlGenerator
- {
- /**
- * @param args
- */
- public static void main(String[] args)
- {
- String sLongUrl = "http://xxx.qzone.qq.com/dsa/fds/gf/hgf/hgf?dsada=dsada"; // 長鏈接
- String[] aResult = shortUrl(sLongUrl);
- // 打印出結果
- for (int i = 0; i < aResult.length; i++)
- {
- System.out.println("[" + i + "]:::" + aResult[i]);
- }
- }
- public static String[] shortUrl(String url)
- {
- // 可以自定義生成 MD5 加密字符傳前的混合 KEY
- String key = "mengdelong";
- // 要使用生成 URL 的字符
- String[] chars = new String[] { "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"
- };
- // 對傳入網址進行 MD5 加密
- String sMD5EncryptResult = (new CMyEncrypt()).md5(key + url);
- String hex = sMD5EncryptResult;
- String[] resUrl = new String[4];
- for (int i = 0; i < 4; i++)
- {
- // 把加密字符按照 8 位一組 16 進制與 0x3FFFFFFF 進行位與運算
- String sTempSubString = hex.substring(i * 8, i * 8 + 8);
- // 這里需要使用 long 型來轉換,因為 Inteper .parseInt() 只能處理 31 位 , 首位為符號位 , 如果不用
- // long ,則會越界
- long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
- String outChars = "";
- for (int j = 0; j < 6; j++)
- {
- // 把得到的值與 0x0000003D 進行位與運算,取得字符數組 chars 索引
- long index = 0x0000003D & lHexLong;
- // 把取得的字符相加
- outChars += chars[(int) index];
- // 每次循環按位右移 5 位
- lHexLong = lHexLong >> 5;
- }
- // 把字符串存入對應索引的輸出數組
- resUrl[i] = outChars;
- }
- return resUrl;
- }
- }
參考:http://iteye.blog.163.com/blog/static/1863080962012111223141936/