com.luues.util.shorts.ShortUrlGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
package com.luues.util.shorts;
import com.luues.util.encryption.MD5Util;
import com.luues.util.logs.LogUtil;
import java.util.UUID;
public class ShortUrlGenerator {
public final static String short_key = "SHORT-URL-version-1.0:";
private final static String[] chars = new String[] { "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"};
/**
* 生成短链接
* @param url (相对文件路径)
* @return
*/
public static String createShortUrl(Object url) {
String sMD5EncryptResult = MD5Util.MD5Encode(short_key + url + UUID.randomUUID(), "UTF-8");
String hex = sMD5EncryptResult;
String resUrl = "";
for ( int i = 0; i < 1; i++) {
String sTempSubString = hex.substring(i * 8, i * 8 + 8);
long lHexLong = 0x3FFFFFFF & Long.parseLong (sTempSubString, 16);
String outChars = "" ;
for ( int j = 0; j < 6; j++) {
long index = 0x0000003D & lHexLong;
outChars += chars[( int ) index];
lHexLong = lHexLong >> 5;
}
resUrl = outChars;
}
LogUtil.debug("shortUrl create success, is : {" + resUrl + "}");
return resUrl;
}
}