Commit 7bf01732 by songxiang

长短链接优化

parent 995f2e1c
package com.pcloud.common.utils.httpclient; package com.pcloud.common.utils.httpclient;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.pcloud.common.utils.rsa.MD5; import com.pcloud.common.utils.rsa.MD5;
import com.pcloud.common.utils.string.StringUtil; import com.pcloud.common.utils.string.StringUtil;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
/** /**
* @author:songx * @author:songx
* @date:2017年12月4日,下午4:58:39 * @date:2017年12月4日,下午4:58:39
*/ */
public class UrlUtils { public class UrlUtils {
/** /**
* *
*/ */
private static final Logger LOGGER = LoggerFactory.getLogger(UrlUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(UrlUtils.class);
/** /**
* 请求地址(新郎接口) * 请求地址(新郎接口)
*/ */
private static final String REQUEST_URL = "https://api.weibo.com/2/short_url/shorten.json?source=1681459862&url_long="; private static final String REQUEST_URL = "https://api.weibo.com/2/short_url/shorten.json?source=1681459862&url_long=";
/** /**
* 请求地址(OWN) * 请求地址(OWN)
*/ */
private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shorten"; private static final String REQUEST_URL2 = "https://rays.5rs.me/convert/v1.0/url/shorten";
/** /**
* 获取短链接(新浪) * 自定义生成 MD5 加密字符传前的混合 KEY
* */
* @param long_url public static final String PRIVATE_KEY = "lgsc1205";
* @return
*/ /**
public static String getShortUrl(String long_url) { * 获取短链接(新浪)
String result = callHttp(long_url); *
if (StringUtil.isEmpty(result)) { * @param long_url
return null; * @return
} */
JSONObject resultObject = JSONObject.parseObject(result); public static String getShortUrl(String long_url) {
JSONArray array = resultObject.getJSONArray("urls"); String result = callHttp(long_url);
JSONObject object = array.getJSONObject(0); if (StringUtil.isEmpty(result)) {
return object.getString("url_short"); return null;
} }
JSONObject resultObject = JSONObject.parseObject(result);
/** JSONArray array = resultObject.getJSONArray("urls");
* 发送请求(新浪) JSONObject object = array.getJSONObject(0);
* return object.getString("url_short");
* @param long_url }
* @return
*/ /**
private static String callHttp(String long_url) { * 发送请求(新浪)
LOGGER.info("【URL】短链接转换(新浪),<START>.[long_url]=" + long_url); *
CloseableHttpClient httpclient = HttpClients.createDefault(); * @param long_url
HttpGet httpGet = new HttpGet(REQUEST_URL + long_url); * @return
String resContent = null; */
try { private static String callHttp(String long_url) {
CloseableHttpResponse response = httpclient.execute(httpGet); LOGGER.info("【URL】短链接转换(新浪),<START>.[long_url]=" + long_url);
// 获取响应实体 CloseableHttpClient httpclient = HttpClients.createDefault();
HttpEntity entity = response.getEntity(); HttpGet httpGet = new HttpGet(REQUEST_URL + long_url);
if (entity != null) { String resContent = null;
resContent = EntityUtils.toString(entity); try {
} CloseableHttpResponse response = httpclient.execute(httpGet);
} catch (IOException e) { // 获取响应实体
LOGGER.error("【URL】短链接转换失败(新浪):" + e.getMessage(), e); HttpEntity entity = response.getEntity();
} if (entity != null) {
LOGGER.info("【URL】短链接转换(新浪),<END>.[resContent]=" + resContent); resContent = EntityUtils.toString(entity);
return resContent; }
} } catch (IOException e) {
LOGGER.error("【URL】短链接转换失败(新浪):" + e.getMessage(), e);
/** }
* 获取短链接(OWN) LOGGER.info("【URL】短链接转换(新浪),<END>.[resContent]=" + resContent);
* return resContent;
* @param long_url }
* @return
*/ /**
public static String getShortUrl4Own(String long_url) { * 获取短链接(OWN)
String result = callHttp4Own(long_url); *
if (StringUtil.isEmpty(result)) { * @param long_url
return null; * @return
} */
JSONObject resultObject = JSONObject.parseObject(result); public static String getShortUrl4Own(String long_url) {
JSONObject dataObject = resultObject.getJSONObject("data"); String result = callHttp4Own(long_url);
return dataObject == null ? null : dataObject.getString("shortUrl"); if (StringUtil.isEmpty(result)) {
} return null;
}
/** JSONObject resultObject = JSONObject.parseObject(result);
* 发送请求(OWN) JSONObject dataObject = resultObject.getJSONObject("data");
* return dataObject == null ? null : dataObject.getString("shortUrl");
* @param long_url }
* @return
*/ /**
private static String callHttp4Own(String long_url) { * 发送请求(OWN)
LOGGER.info("【URL】短链接转换(OWN),<START>.[long_url]=" + long_url); *
if (StringUtil.isEmpty(long_url)) { * @param long_url
return null; * @return
} */
CloseableHttpClient httpclient = HttpClients.createDefault(); private static String callHttp4Own(String long_url) {
HttpPost httpPost = new HttpPost(REQUEST_URL2); LOGGER.info("【URL】短链接转换(OWN),<START>.[long_url]=" + long_url);
String resContent = null; if (StringUtil.isEmpty(long_url)) {
try { return null;
//设置参数到请求对象中 }
JSONObject jsonObject = new JSONObject(); CloseableHttpClient httpclient = HttpClients.createDefault();
jsonObject.put("originUrl", long_url); HttpPost httpPost = new HttpPost(REQUEST_URL2);
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), Charset.forName("UTF-8")); String resContent = null;
httpPost.setEntity(stringEntity); try {
//设置header信息 // 设置参数到请求对象中
//指定报文头【Content-type】、【User-Agent】 JSONObject jsonObject = new JSONObject();
httpPost.setHeader("Content-type", "application/json;charset=UTF-8"); jsonObject.put("originUrl", long_url);
CloseableHttpResponse response = httpclient.execute(httpPost); StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), Charset.forName("UTF-8"));
// 获取响应实体 httpPost.setEntity(stringEntity);
HttpEntity entity = response.getEntity(); // 设置header信息
if (entity != null) { // 指定报文头【Content-type】、【User-Agent】
resContent = EntityUtils.toString(entity); httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
} CloseableHttpResponse response = httpclient.execute(httpPost);
} catch (IOException e) { // 获取响应实体
LOGGER.error("【URL】短链接转换失败(OWN):" + e.getMessage(), e); HttpEntity entity = response.getEntity();
} if (entity != null) {
LOGGER.info("【URL】短链接转换(OWN),<END>.[resContent]=" + resContent); resContent = EntityUtils.toString(entity);
return resContent; }
} } catch (IOException e) {
LOGGER.error("【URL】短链接转换失败(OWN):" + e.getMessage(), e);
/** }
* 缩短链接 LOGGER.info("【URL】短链接转换(OWN),<END>.[resContent]=" + resContent);
* return resContent;
* @param url }
* @return
*/ /**
public static String[] shortenUrl(String url) { * 对传入的链接进行加密
// 可以自定义生成 MD5 加密字符传前的混合 KEY *
String key = "lgsc1205"; * @param url
// 要使用生成 URL 的字符 * @return
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", public static String[] shortenUrl(String url) {
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", // 对传入网址进行 MD5 加密
"W", "X", "Y", "Z" String hex = MD5.getMD5Str(PRIVATE_KEY + url);
return shortenUrlMd5(hex);
}; }
// 对传入网址进行 MD5 加密
String hex = MD5.getMD5Str(key + url); /**
* 缩短链接
String[] resUrl = new String[4]; *
for (int i = 0; i < 4; i++) { * @param url
* @return
// 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算 */
String sTempSubString = hex.substring(i * 8, i * 8 + 8); public static String[] shortenUrlMd5(String hex) {
// 要使用生成 URL 的字符
// 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
// 如果不用long ,则会越界 "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16); "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
String outChars = ""; "W", "X", "Y", "Z" };
for (int j = 0; j < 7; j++) {
// 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引 String[] resUrl = new String[4];
long index = 0x0000003D & lHexLong; for (int i = 0; i < 4; i++) {
// 把取得的字符相加 // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算
outChars += chars[(int) index]; String sTempSubString = hex.substring(i * 8, i * 8 + 8);
// 每次循环按位右移 4 位
lHexLong = lHexLong >> 4; // 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 ,
} // 如果不用long ,则会越界
// 把字符串存入对应索引的输出数组 long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16);
resUrl[i] = outChars; String outChars = "";
} for (int j = 0; j < 7; j++) {
return resUrl; // 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引
} long index = 0x0000003D & lHexLong;
// 把取得的字符相加
} outChars += chars[(int) index];
// 每次循环按位右移 4 位
lHexLong = lHexLong >> 4;
}
// 把字符串存入对应索引的输出数组
resUrl[i] = outChars;
}
return resUrl;
}
}
...@@ -10,8 +10,6 @@ import org.slf4j.LoggerFactory; ...@@ -10,8 +10,6 @@ import org.slf4j.LoggerFactory;
import com.pcloud.common.utils.string.StringUtil; import com.pcloud.common.utils.string.StringUtil;
/** /**
* MD5 * MD5
*/ */
...@@ -66,9 +64,22 @@ public class MD5 { ...@@ -66,9 +64,22 @@ public class MD5 {
else else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); 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();
}
/** /**
* MD5字符串,全小写 * MD5字符串,全小写
* *
...@@ -111,9 +122,9 @@ public class MD5 { ...@@ -111,9 +122,9 @@ public class MD5 {
return md5StrBuff.toString().toUpperCase(); 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(MD5.getMD5Str("chenjianhua"));
System.out.println(DigestUtils.md5Hex("chenjianhua").toUpperCase()); System.out.println(DigestUtils.md5Hex("chenjianhua").toUpperCase());
} }
} }
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