Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert long links into short links in java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to convert long links into short links in java. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

First, the realization principle

1. The long URL is generated by md5 algorithm to generate 32-bit signature string, which is divided into 4 segments, each with 8 characters.

two。 For these four segments of loop processing, take the 8 characters of each segment and regard it as the bit and operation of hexadecimal string and 0x3fffffff (30 bits 1), and ignore more than 30 bits. It's no use if it's too much, because it's going to be divided into six paragraphs. Pay attention to the use of long variables (you know the length problem)

3. The 30-bit characters obtained in each segment (displayed in the long decimal system in the background) are divided into 6 segments, and every 5 bits are 0x0000003D with the character array by shift operation to get its index in the character array and take out the spelling string.

4. Such a md5 string can get four 6-bit strings, any one of which can be used as the short url address of the long url.

Second, code implementation

Import org.apache.commons.codec.digest.DigestUtils; public class ShortUrl {public static void main (String [] args) {/ / persistent connection String longUrl = "http://data.13322.com/basket/team/27_0_1.html"; / / converted to short link 6-bit code String [] shortCodeArray = shortUrl (longUrl); for (int I = 0; I

< shortCodeArray.length; i++) { System.out.println(shortCodeArray[i]);// 任意一个都可以作为短链接码 } } public static String[] shortUrl(String url) { // 可以自定义生成 MD5 加密字符传前的混合 KEY String key = ""; // 要使用生成 URL 的字符 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", "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" }; // 对传入网址进行 MD5 加密 String sMD5EncryptResult = DigestUtils.md5Hex(key + url); String hex = sMD5EncryptResult; String[] resUrl = new String[4]; for (int i = 0; i < 4; i++) { // 把加密字符按照 8 位一组 16 进制与 0x3FFFFFFF 进行位与运算 String sTempSubString = hex.substring(i * 8, i * 8 + 8); // 这里需要使用 long 型来转换,因为 Inteper .parseInt() 只能处理 31 位 , 首位为符号位 , 如果不用 // long ,则会越界 long lHexLong = 0x3FFFFFFF & Long.parseLong(sTempSubString, 16); String outChars = ""; for (int j = 0; j < 6; j++) { // 把得到的值与 0x0000003D 进行位与运算,取得字符数组 chars 索引 long index = 0x0000003D & lHexLong; // 把取得的字符相加 outChars += chars[(int) index]; // 每次循环按位右移 5 位 lHexLong = lHexLong >

> 5;} / / put the string into the output array of the corresponding index resUrl [I] = outChars;} return resUrl;}}

Third, jump principle

When we generate a short link, we only need to store the mapping between the original link and the short link in the table (database or NoSql).

When we visit the short link, we only need to find the original link in the mapping relationship to jump to the original link.

Conversion principle: convert the original url into a 6-bit short code through a series of ways (any way as long as it can not be repeated); store the long and short links in the database to form a corresponding relationship; when accessing the short links, find the corresponding long links in the database, and redirect the access to the original url (if your conversion method can be restored, you can also do without the database, but you must make sure that the converted short code cannot be repeated.) the database looks like this.

@ RequestMapping ("/ {shortUrl}") public ModelAndView jumpLongLink (HttpServletRequest request, ModelAndView mav, @ PathVariable ("shortUrl") String shortUrl) {String longUrl = ""; String longurl = shorturlService.restoreUrl (shortUrl); if (longURL null) {longUrl = longurl;} mav.setViewName ("redirect:" >

I won't talk about the first line of comments. {shortUrl} this is the dynamically generated short link. The shorturlService.restoreUrl () method is passed in the short link to the long link corresponding to the query in the library, that is, the real address. Then forward and redirect to the real address. Complete short link local access: 127.0.0.1 / port number / short link, such as: 127.0.0.1/8888/2MnQFj, online access address: domain name / short link, such as: www.baidu.com/2MnQFj, if the project has an interceptor, because the short link is dynamic, you need to add a layer of path in front, like this: 127.0.0.1/8888/go/2MnQFj and then it is done.

The above is how to convert long links into short links in the java shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report