java 类似微博的短链接生成方式

发布时间:2018-03-07作者:laosun阅读(2464)

java

短内容生成器(常用于类似微博短链接的生成) 源码分享

    //                         _ooOoo_  
    //                        o8888888o  
    //                        88" . "88  
    //                        (| -_- |)  
    //                         O\ = /O  
    //                     ____/`---'\____  
    //                   .   ' \\| |// `.  
    //                    / \\||| : |||// \  
    //                  / _||||| -:- |||||- \  
    //                    | | \\\ - /// | |  
    //                  | \_| ''\---/'' | |  
    //                   \ .-\__ `-` ___/-. /  
    //                ___`. .' /--.--\ `. . __  
    //             ."" '< `.___\__/___.' >'"".  
    //            | | : `- \`.;`\ _ /`;.`/ - ` : | |  
    //              \ \ `-. \_ __\ /__ _/ .-` / /  
    //      ======`-.____`-.___\_____/___.-`____.-'======  
    //                         `=---='  
    //
    //      .............................................  
    //               佛祖保佑             永无BUG 
    //       佛曰:  
    //               写字楼里写字间,写字间里程序员;  
    //               程序人员写程序,又拿程序换酒钱。  
    //               酒醒只在网上坐,酒醉还来网下眠;  
    //               酒醉酒醒日复日,网上网下年复年。  
    //               但愿老死电脑间,不愿鞠躬老板前;  
    //               奔驰宝马贵者趣,公交自行程序员。  
    //               别人笑我忒疯癫,我笑自己命太贱;  
    //               不见满街漂亮妹,哪个归得程序员? 
    
    import java.security.MessageDigest;
    
    /**
     * 短内容生成器(常用于类似微博短链接的生成)
     * @author sun
     * @date 2018年3月06日 下午12:52:12
     */
    public class ShortTextUtils {
    
        private static final String KEY = "sunjs";
    
        /**
         * 生成方法
         * @author sun
         * @date 2018年3月06日 下午12:52:19
         * @param str
         * @return
         */
        public static String execute(String str) {
            for (String st : ShortText(str)) {
                return st;
            }
            return str;
        }
    
        private static String[] ShortText(String string) {
            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 = 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 final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
                "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    
        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];
        }
    
        private 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) {
            System.out.println(execute("http://www.baidu.com"));
            System.out.println(execute("你好"));
            System.out.println(execute("MessageDigest md = MessageDigest.getInstanceMessageDigest md = MessageDigest.getInstance"));
        }
        
    }


1 +1

版权声明

 Java  源码

 请文明留言

0 条评论