推广 热搜: page  红书  搜索  小红  哪些  考试  数据  论文  数据分析  关键词 

java短链接分享方案_java 短链接生成

   日期:2025-01-03     移动:https://sicmodule.kub2b.com/mobile/quote/18447.html

package shorurl;

import org.apache.commons.codec.digest.DigestUtils;

import org.apache.commons.lang.StringUtils;

publicclassShortUrl{

privatestaticfinalint BINARY =0x2;

privatestaticfinalint NUMBER_61 =0x0000003d;

staticfinalchar[] DIGITS ={'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','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'};

protectedString shorten(String longUrl){

String shortUrl = shorten(longUrl,5);

if(StringUtils.isEmpty(shortUrl)){

shortUrl = shorten(longUrl,6);

}

return shortUrl;

}

publicString shorten(String longUrl,int urlLength){

if(urlLength <0|| urlLength >6){

thrownewIllegalArgumentException("the length of url must be between 0 and 6");

}

String md5Hex =DigestUtils.md5Hex(longUrl);

// 6 digit binary can indicate 62 letter & number from 0-9a-zA-Z

int binaryLength = urlLength *6;

long binaryLengthFixer =Long.valueOf(StringUtils.repeat("1", binaryLength), BINARY);

for(int i =0; i <4; i++){

String subString =StringUtils.substring(md5Hex, i *8,(i +1)*8);

subString =Long.toBinaryString(Long.valueOf(subString,16)& binaryLengthFixer);

subString =StringUtils.leftPad(subString, binaryLength,"0");

StringBuilder sbBuilder =newStringBuilder();

for(int j =0; j < urlLength; j++){

String subString2 =StringUtils.substring(subString, j *6,(j +1)*6);

int charIndex =Integer.valueOf(subString2, BINARY)& NUMBER_61;

sbBuilder.append(DIGITS[charIndex]);

}

String shortUrl = sbBuilder.toString();

return shortUrl;

}

returnnull;

}

本文地址:https://sicmodule.kub2b.com/quote/18447.html     企库往 https://sicmodule.kub2b.com/ , 查看更多

特别提示:本信息由相关用户自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


0相关评论
相关最新动态
推荐最新动态
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2020018471号