算法一:
最容易想到的算法可能是利用md5类的加密算法,然后针对加密后的字符串进行处理。
1)将长网址md5生成32位签名串,分为4段, 每段8个字节;public class ShortUrlUtil { static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String shortUrl(String url) throws Exception { MessageDigest messagedigest = MessageDigest.getInstance("MD5"); messagedigest.update(url.getBytes()); String result = bufferToHex(messagedigest.digest()); String resUrl = new String(""); for (int i = 0; i < 8; i++) { String tmpString = result.substring(i * 4, i * 4 + 4); long hexLong = 0x3FFFFFFF & Long.parseLong(tmpString, 16); resUrl += hexDigits[Integer.valueOf((hexLong % 16) + "")] + ""; } return resUrl; } private static String bufferToHex(byte bytes[]) { return bufferToHex(bytes, 0, bytes.length); } private static String bufferToHex(byte bytes[], int m, int n) { StringBuffer stringbuffer = new StringBuffer(2 * n); int k = m + n; for (int l = m; l < k; l++) { appendHexPair(bytes[l], stringbuffer); } return stringbuffer.toString(); } private static void appendHexPair(byte bt, StringBuffer stringbuffer) { char c0 = hexDigits[(bt & 0xf0) >> 4]; char c1 = hexDigits[bt & 0xf]; stringbuffer.append(c0); stringbuffer.append(c1); } public static void main(String[] args) { String sLongUrl = "http://weibo.com/taobaotianshui?wvr=5&wvr=5&lf=reg"; String shortUrl; try { shortUrl = shortUrl(sLongUrl); System.out.println(sLongUrl + " ===> " + shortUrl); } catch (Exception e) { e.printStackTrace(); } } }
当生成短网址链接之后,只需要在表中(数据库或者类NoSql的K-V存储)存储原始链接与短链接的映射关系即可。