栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java短链接生成、长链接转换短链接,永久短链接

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java短链接生成、长链接转换短链接,永久短链接

什么是短链接?

就是把普通网址,转换成比较短的网址。比如: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
            fastjson
            1.2.78
        

2、发送请求是使用的Jsoup,也可以用其他方式代替

        
            org.jsoup
            jsoup
            1.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;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1025027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号