博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
短网址
阅读量:5021 次
发布时间:2019-06-12

本文共 2291 字,大约阅读时间需要 7 分钟。

短网址就是很简短的网址,目前有两种实现方式。1.自有短网址域名或第三方,生成短网址后,自动解析。2.生成短网址后,保存短网址与原网址映射关系,由本系统域名对外转发并跳转。使用第二种方法实现方式如下:

1 public class ShortUrl {  2     public static String ShortText(String originalUrl){  3         String key = "com.maidepiao";                 4         String[] chars = new String[]{          5             "a","b","c","d","e","f","g","h",  6             "i","j","k","l","m","n","o","p",  7             "q","r","s","t","u","v","w","x",  8             "y","z","0","1","2","3","4","5",  9             "6","7","8","9","A","B","C","D", 10             "E","F","G","H","I","J","K","L", 11             "M","N","O","P","Q","R","S","T", 12             "U","V","W","X","Y","Z" 13         }; 14         String hex = Encript.md5(key + originalUrl); 15         int hexLen = hex.length(); 16         int subHexLen = hexLen / 8; 17         String[] ShortStr = new String[4]; 18          19         for (int i = 0; i < subHexLen; i++) { 20             String outChars = ""; 21             int j = i + 1; 22             String subHex = hex.substring(i * 8, j * 8); 23             long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16); 24              25             for (int k = 0; k < 6; k++) { 26                 int index = (int) (Long.valueOf("0000003D", 16) & idx); 27                 outChars += chars[index]; 28                 idx = idx >> 5; 29             } 30             ShortStr[i] = outChars; 31         } 32         Random random=new Random();33         int j=random.nextInt(4);34         /**35          * 将短连接与原链接的映射关系保存至redis,由系统向外重定向36          */37         JedisUtils.set("shortURL:"+ShortStr[j], originalUrl, 0);38         return ShortStr[j]; 39     } 40 }

生成短连接后,根据短连接码请求自有系统,和普通的request一样,然后重定向跳转原路径

1 @Controller 2 @RequestMapping("") 3 public class ShortUrlRoute extends BaseController{ 4     //private static final Logger logger = LogManager.getLogger(ShortUrlRoute.class); 5      6     @RequestMapping(value= "/{shortUrlKey}" )   7     public String message(@PathVariable String shortUrlKey,HttpServletRequest request,HttpServletResponse response) { 8         return "redirect:"+JedisUtils.get("shortURL:"+shortUrlKey,""); 9     }10 }

采用的是springMVC请求,使用redis作为中间的载体,请求短网址时,根据短网址在redis找到原路径,然后跳转即可。

喜欢请关注微信公众号:码农小麦

转载于:https://www.cnblogs.com/render-inside/p/5852326.html

你可能感兴趣的文章
MYSQL(三)
查看>>
浏览器内核
查看>>
用dtree实现树形菜单
查看>>
相遇点对 & 数点问题
查看>>
百练2815 城堡问题
查看>>
javascript closure 闭包 事件绑定
查看>>
指针与const
查看>>
Angular结构简单介绍
查看>>
大道至简---读书随笔3
查看>>
POJ 1003 Hangover
查看>>
POJ 2856 计算邮资
查看>>
NDK的安装和下载
查看>>
C#学习笔记
查看>>
SerialTest正式入住CodePlex
查看>>
CSS 设计中firefox IE Chrome兼容问题
查看>>
java2 实用教程第四章
查看>>
2017.12.1T19_B2_5zuoye
查看>>
在编译器中调试spark程序处理
查看>>
css3 iphone开关 移动端开关、按钮、input
查看>>
信息论随笔
查看>>