Commit 7bf01732 by songxiang

长短链接优化

parent 995f2e1c
......@@ -40,6 +40,11 @@ public class UrlUtils {
private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shorten";
/**
* 自定义生成 MD5 加密字符传前的混合 KEY
*/
public static final String PRIVATE_KEY = "lgsc1205";
/**
* 获取短链接(新浪)
*
* @param long_url
......@@ -112,13 +117,13 @@ public class UrlUtils {
HttpPost httpPost = new HttpPost(REQUEST_URL2);
String resContent = null;
try {
//设置参数到请求对象中
// 设置参数到请求对象中
JSONObject jsonObject = new JSONObject();
jsonObject.put("originUrl", long_url);
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), Charset.forName("UTF-8"));
httpPost.setEntity(stringEntity);
//设置header信息
//指定报文头【Content-type】、【User-Agent】
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
CloseableHttpResponse response = httpclient.execute(httpPost);
// 获取响应实体
......@@ -134,27 +139,32 @@ public class UrlUtils {
}
/**
* 缩短链接
* 对传入的链接进行加密
*
* @param url
* @return
*/
public static String[] shortenUrl(String url) {
// 可以自定义生成 MD5 加密字符传前的混合 KEY
String key = "lgsc1205";
// 对传入网址进行 MD5 加密
String hex = MD5.getMD5Str(PRIVATE_KEY + url);
return shortenUrlMd5(hex);
}
/**
* 缩短链接
*
* @param url
* @return
*/
public static String[] shortenUrlMd5(String hex) {
// 要使用生成 URL 的字符
String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
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"
};
// 对传入网址进行 MD5 加密
String hex = MD5.getMD5Str(key + url);
"W", "X", "Y", "Z" };
String[] resUrl = new String[4];
for (int i = 0; i < 4; i++) {
// 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
String sTempSubString = hex.substring(i * 8, i * 8 + 8);
......
......@@ -10,8 +10,6 @@ import org.slf4j.LoggerFactory;
import com.pcloud.common.utils.string.StringUtil;
/**
* MD5
*/
......@@ -66,7 +64,20 @@ public class MD5 {
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString().toUpperCase();
return md5StrBuff.toString();
}
/**
* MD5字符串,全大写
*
* @param str
* @return
*/
public static String getMD5StrUpper(String str) {
if (StringUtil.isEmpty(str)) {
return str;
}
return getMD5Str(str).toUpperCase();
}
/**
......@@ -111,7 +122,7 @@ public class MD5 {
return md5StrBuff.toString().toUpperCase();
}
public static void main(String[] args){
public static void main(String[] args) {
System.out.println(MD5.getMD5Str("chenjianhua"));
System.out.println(DigestUtils.md5Hex("chenjianhua").toUpperCase());
......
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment