首页

短链接生成工具生成TinyUrlUtil - 将长连接转换等长短链接

标签:短链接     发布时间:2024-09-04   
package com.tools.common.util;@b@@b@import org.springframework.stereotype.Component;@b@@b@/**@b@ * 1)将长网址用md5算法生成32位签名串,分为4段,,每段8个字符;@b@ *@b@ * 2)对这4段循环处理,取每段的8个字符, 将他看成16进制字符串与0x3fffffff(30位1)的位与操作。超过30位的忽略处理。@b@ *@b@ * 3)将每段得到的这30位又分成6段,每5位的数字作为字母表的索引取得特定字符,依次进行获得6位字符串;@b@ *@b@ * 4)这样一个md5字符串能够获得4个6位串。取里面的随意一个就可作为这个长url的短url地址。@b@ */@b@@Component@b@public class TinyUrlUtil {@b@@b@    public String[] ShortText(String string){@b@        String key = "qwe20190909";//自己定义生成MD5加密字符串前的混合KEY@b@        String[] chars = new String[]{//要使用生成URL的字符@b@                "a","b","c","d","e","f","g","h",@b@                "i","j","k","l","m","n","o","p",@b@                "q","r","s","t","u","v","w","x",@b@                "y","z","0","1","2","3","4","5",@b@                "6","7","8","9","A","B","C","D",@b@                "E","F","G","H","I","J","K","L",@b@                "M","N","O","P","Q","R","S","T",@b@                "U","V","W","X","Y","Z"@b@        };@b@@b@        String hex = EncriptUtil.md5(key + string);@b@        int hexLen = hex.length();@b@        int subHexLen = hexLen / 8;@b@        String[] ShortStr = new String[4];@b@        for (int i = 0; i < subHexLen; i++) {@b@            String outChars = "";@b@            int j = i + 1;@b@            String subHex = hex.substring(i * 8, j * 8);@b@            long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16);@b@@b@            for (int k = 0; k < 6; k++) {@b@                int index = (int) (Long.valueOf("0000003D", 16) & idx);@b@                outChars += chars[index];@b@                idx = idx >> 5;@b@            }@b@            ShortStr[i] = outChars;@b@        }@b@@b@        return ShortStr;@b@    }@b@}


  • <<相关内容>>
<<热门下载>>