URL short address compression algorithm micro-blog short address principle analysis (re-turn and Java implementation)

Source: Internet
Author: User

The main is a Sina Weibo, the site was sent to see the site was compressed, only interested in this, so check the check, find these

The short address principle of short address in URL compression algorithm

And its Java implementation of the blog

URL short address compression algorithm micro-blog short address principle analysis (Java implementation)

The first blog, left a question, mainly think of four of the compressed address to take one, before the cyclic calculation is not a waste of computational power.

No more nonsense, here is to post their own Java validation implementation code:

Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;

Import java.io.*;
		public class shorturl{public static void Main (string[] args) {String url = ' http://www.sunchis.com ';
		For (string string:shorttext (URL)) {print (string);			} public static string[] Shorttext (String string) {string key = ' Xuliang '; Custom generated MD5 cryptographic string mixed key string[] chars = new string[]{//To use the character "a" to generate the URL, "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 = 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",) & IDX);//There is no requirement to take 5 digits here.
				Not to take a continuous 5-bit outchars + = Chars[index];
			IDX = idx >> 5;
		} Shortstr[i] = Outchars;
	return shortstr;
	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;
	private static void print (Object messagr) {System.out.println (MESSAGR);
 }
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.