长链接转短链接,短链接跳转对应页面

生成长链接对应的值,将其作为key,长链接作为value保存到redis中,然后设计一个接口,根据key从redis中获取长链接,通过重定向访问长链接对应的页面

引入pom依赖

 		<dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.12</version>
        </dependency> 
        

生成短链接的工具类

/**
 * @Author ybl
 * @Date 2023/9/25 15:34
 * @Description 1、将长网址 md5 生成 32 位签名串,分为 4 段, 每段 8 个字节
 * 2、对这四段循环处理, 取 8 个字节, 将他看成 16 进制串与 0x3fffffff(30位1) 与操作, 即超过 30 位的忽略处理
 * 3、这 30 位分成 6 段, 每 5 位的数字作为字母表的索引取得特定字符, 依次进行获得 6 位字符串
 * 4、总的 md5 串可以获得 4 个 6 位串,取里面的任意一个就可作为这个长 url 的短 url 地址
 **/
public class ShortUrlUtil {

    public static final char[] chars = new char[]{'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',
            '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'};
    public static final String key = "135796";

    /**
     * 生成短链的id
     * @param url 地址
     * @param num 需要生成的短链id的个数
     * @return 数组
     * **/
    public static String[] shortUrl(String url,int num) {
        String sMD5EncryptResult = DigestUtils.md5Hex(key + url);
        String hex = sMD5EncryptResult;
        String[] resUrl = new String[4];

        for (int i = 0; i < num; i++) {
            long lHexLong = 0x3FFFFFFF & Long.parseLong(hex.substring(i * 8, i * 8 + 8), 16);
            StringBuilder outChars = new StringBuilder();

            for (int j = 0; j < 6; j++) {
                long index = 0x0000003D & lHexLong;
                outChars.append(chars[(int) index]);
                lHexLong >>= 5;
            }
            resUrl[i] = outChars.toString();
        }
        return resUrl;
    }
}

长链接生成短链接

以访问某一类详情页面的url生成短链接为例


    @Autowired
    private RedisCache redisCache;
    
    /**
     * 拼接长连接获取对应的短连接
     * @param urlPrefix 访问地址的前缀
     * @param detailId 详情的id
     * @return 短链地址
     * **/
    public String getShortUrl(String urlPrefix,String detailId){
        String detailUrl  = "http://XXXX.com/detail?detailId=" + detailId;

        String[] shortUrlId = ShortUrlUtil.shortUrl(detailUrl, 1);

        redisCache.setCacheObject(shortUrlId[0],detailUrl);
        String shortUrl = urlPrefix + "/short/" + shortUrlId[0];
        return shortUrl;
    }

通过短链接访问长链接

@RestController
@RequestMapping("/short")
public class ShortUrlController {

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/{id}")
    public RedirectView getUrl(@PathVariable String id){
        return new RedirectView(redisCache.getCacheObject(id));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值