一、效果就是把普通网址,转换成比较短的网址。比如:www.cyho.top/SRNj6 这种,很短,但进入链接后会发现跳转到一个链接很长的页面中,在微博这些限制字数的应用里。好处不言而喻。短、字符少、美观、便于发布、传播。
复制链接查看效果: www.cyho.top/SRNj6
链接永久有效,若需要https,可自己再套一层域名解析
public static void main(String[] args) throws IOException {
String longUrl = "https://blog.csdn.net/qq_41175917/article/details/118785333";
String aesKey = "023382397f9848458967d2b5b9462205";
Long userId = 18293l;
SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
JSONObject jsonObject = new JSONObject();
JSONObject head = new JSONObject();
head.put("userId", userId);
head.put("time", AesUtils.encrypt(time.format(new Date()), aesKey));
jsonObject.put("head", head);
jsonObject.put("longUrl", AesUtils.encrypt(longUrl,aesKey));
Connection.Response execute = Jsoup.connect("http://www.cyho.top/shortUrl/set")
.requestBody(jsonObject.toJSONString())
.ignoreContentType(true).method(Connection.Method.POST)
.header("content-type", "application/json;charset=UTF-8").execute();
System.out.println(execute.body());
// 响应:{"msg":"success","code":0,"shortUrl":"www.cyho.top/SRNj6"}
} catch (Exception e) {
e.printStackTrace();
}
}
三、工具类
1、其中JSON类是com.alibaba的,可以随便用其他代替,都一样的
com.alibaba fastjson1.2.78
2、发送请求是使用的Jsoup,也可以用其他方式代替
org.jsoup jsoup1.12.1
3、aes加密工具类
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class AesUtils {
// 加密算法
private static final String ENCRY_ALGORITHM = "AES";
// 设置加密字符集
private static final String CHARACTER = "UTF-8";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
public static String encrypt(String content, String password) throws Exception {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = content.getBytes(CHARACTER);
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
byte[] result = cipher.doFinal(byteContent);
return parseByte2HexStr(result);
}
public static String decrypt(String content, String password) throws Exception {
try {
//实例化
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
//使用密钥初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
//执行操作
byte[] result = cipher.doFinal(parseHexStr2Byte(content));
return new String(result, CHARACTER);
} catch (Exception e){
throw new Exception("解密失败");
}
}
private static SecretKeySpec getSecretKey(final String password) throws Exception {
//返回生成指定算法密钥生成器的 KeyGenerator 对象
KeyGenerator kg = null;
kg = KeyGenerator.getInstance(ENCRY_ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes());
kg.init(128, secureRandom);
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), ENCRY_ALGORITHM);
}
public static String parseByte2HexStr(byte buf[]) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) {
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}



