
{"id":1176,"date":"2019-04-24T12:07:55","date_gmt":"2019-04-24T04:07:55","guid":{"rendered":"http:\/\/www.jiandaima.com\/blog\/?p=1176"},"modified":"2019-04-24T12:08:41","modified_gmt":"2019-04-24T04:08:41","slug":"java%e7%9f%ad%e9%93%be%e6%8e%a5%e7%9a%84%e7%ae%80%e5%8d%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/www.jiandaima.com\/blog\/archives\/1176.html","title":{"rendered":"java\u77ed\u94fe\u63a5\u7684\u7b80\u5355\u5b9e\u73b0"},"content":{"rendered":"<p>\u5f88\u4e45\u6ca1\u66f4\u65b0\u4e86\uff0c\u52a0\u5165\u4e86\u521b\u4e1a\u516c\u53f8\uff0c\u4ece\u65e0\u5230\u6709\u7684\u6784\u5efa\u4e00\u5957\u4ea7\u54c1\uff0c\u4ece\u4e00\u5f00\u59cb\u7684\u7801\u6846\u67b6\u5230\u73b0\u5728\u601d\u8003\u5982\u4f55\u4f18\u5316\u4ea7\u54c1\u67b6\u6784\uff0c\u9002\u5e94\u66f4\u591a\u7684\u5f39\u6027\u9700\u6c42\u548c\u66f4\u5927\u7684\u6570\u636e\u91cf\u3002\u65f6\u95f4\u771f\u7684\u662f\u4e0d\u591f\u7528\uff0c\u51e0\u4e4e\u5feb\u534a\u5e74\u6ca1\u6709\u66f4\u65b0\u535a\u5ba2\u3002\u8fd1\u671f\u4ea7\u54c1\u6162\u6162\u8d70\u4e0a\u6b63\u8f68\uff0c\u53c8\u6709\u5c0f\u4f19\u4f34\u52a0\u5165\uff0c\u6240\u4ee5\u62bd\u51fa\u70b9\u65f6\u95f4\u603b\u7ed3\u4e00\u4e0b\u8fd9\u5c0f\u534a\u5e74\u5185\u9047\u5230\u7684\u5751\u548c\u8d70\u8fc7\u7684\u8def\u3002<br \/>\n<img decoding=\"async\" class=\"alignnone size-full wp-image-1177 \" src=\"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png\" alt=\"\" \/><\/p>\n<p>\u8bf4\u4e00\u8bf4\u6700\u8fd1\u505a\u7684\u4e00\u4e2a\u77ed\u94fe\u670d\u52a1\uff0c\u77ed\u94fe\u670d\u52a1\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5176\u5b9e\u5c31\u662f\u4e00\u5f20\u8868\u6216\u8005\u8bf4\u662f\u4e00\u4e2amap\uff1a<br \/>\n+\u2014\u2014-+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+<br \/>\n| \u77ed\u94fe | \u957f\u94fe |<br \/>\n+\u2014\u2014-+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+<br \/>\n| 7Y65s | https:\/\/www.google.com |<br \/>\n+\u2014\u2014-+\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2013+<\/p>\n<p>\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u7ed3\u6784\uff0c\u5f53\u7528\u6237\u8bbf\u95ee your-domain\/7Y65s, \u53bb\u8868\u91cc\u67e5\u8be2\u4e00\u4e0b\u5bf9\u5e94\u7684\u957f\u94fe\uff0c301\u5230\u8be5\u94fe\u63a5\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4f18\u5316<br \/>\n\u57fa\u672c\u7684\u601d\u8def\u786e\u5b9a\u540e\uff0c\u6211\u4eec\u4e00\u70b9\u4e00\u70b9\u6765\u4f18\u5316\u3002<\/p>\n<p>\u9996\u5148\u5206\u6790\u5927\u91cf\u67e5\u8be2\u80af\u5b9a\u4f1a\u51fa\u73b0\u5728\u901a\u8fc7\u77ed\u94fe\u627e\u957f\u94fe\u8fd9\u6bb5\u903b\u8f91\u4e2d\uff0c\u5982\u679c\u7528\u6570\u636e\u5e93\u80af\u5b9a\u5f71\u54cd\u6548\u7387\uff0c\u7f13\u5b58\u52bf\u5728\u5fc5\u884c\uff0c\u800c\u4e14\u4e00\u65e6\u77ed\u94fe\u751f\u6210\u57fa\u672c\u662f\u4e0d\u4f1a\u53d8\u7684\uff0c\u6240\u4ee5\u4e5f\u4e0d\u5b58\u5728\u5931\u6548\u95ee\u9898\uff08\u8fd9\u91cc\u53ef\u80fd\u4f1a\u6709\u4e2a\u6279\u91cfArchive\u7684\u5904\u7406\uff09\u3002\u5728\u6211\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u9009\u62e9\u4e86redis\u4f5c\u4e3a\u7f13\u5b58\uff0c\u53cd\u6b63\u662fkey-value\u7684\u5176\u4ed6\u7f13\u5b58\u7ec4\u4ef6\u80af\u5b9a\u4e5f\u80fd\u505a\u3002\u9009\u7528redis\u7684\u539f\u56e0\u662f\u8fd8\u53ef\u4ee5\u7528INCR\u6765\u7edf\u8ba1\u8bbf\u95ee\u91cf\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">redisTemplate.opsForHash().put(&quot;short-urls:&quot; + slug, &quot;long_url&quot;, url);\r\nredisTemplate.opsForHash().increment(&quot;short-urls:&quot; + slug, VISITS_FIELD, 1L);<\/pre>\n<p>\u67e5\u8be2\u8fd9\u4e00\u4fa7\u505a\u5b8c\uff0c\u5230\u4e86\u751f\u6210\u77ed\u94fe\u8fd9\u4e00\u5757\uff0c\u8fd9\u91cc\u5b9e\u9645\u4e0a\u662f\u521a\u521a\u8bf4\u7684\u90a3\u5f20\u8868\u7684create\u64cd\u4f5c\u3002\u8fd9\u91cc\u6709\u4e2a\u57fa\u672c\u903b\u8f91\u662f\uff1a\u5f53\u4e00\u4e2a\u957f\u94fe\u8fd8\u6ca1\u6709\u77ed\u94fe\u7684\u65f6\u5019\uff0c\u6211\u4eec\u751f\u6210\u5b83\u5e76\u8fd4\u56de\uff0c\u5982\u679c\u5df2\u7ecf\u5b58\u5728\uff0c\u76f4\u63a5\u83b7\u53d6\u8fd4\u56de\u3002<\/p>\n<p>\u751f\u6210\u7684\u7b97\u6cd5\u76f4\u63a5\u8d34\u4ee3\u7801\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\r\npackage io.naza.urlshortener.generator;\r\n\r\nimport org.apache.commons.codec.digest.DigestUtils;\r\n\r\npublic class UrlShortHelper {\r\n\r\n    private final static int LENGTH = 6;\r\n\r\n    private static char&#x5B;] DIGITS = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;.toCharArray();\r\n\r\n    public static String&#x5B;] shorten(String url) {\r\n        String key = &quot;SECRET&quot;; \/\/ \u81ea\u5b9a\u4e49\u751f\u6210MD5\u52a0\u5bc6\u5b57\u7b26\u4e32\u524d\u7684\u6df7\u5408KEY\r\n        String hex = DigestUtils.md5Hex(key + url);\r\n        int hexLen = hex.length();\r\n        int subHexLen = hexLen \/ 8;\r\n        String&#x5B;] shortStr = new String&#x5B;subHexLen];\r\n\r\n        for (int i = 0; i &lt; subHexLen; i++) {\r\n            StringBuilder outChars = new StringBuilder();\r\n            int j = i + 1;\r\n            String subHex = hex.substring(i * 8, j * 8);\r\n            long idx = Long.valueOf(&quot;3FFFFFFF&quot;, 16) &amp; Long.valueOf(subHex, 16);\r\n            for (int k = 0; k &lt; LENGTH; k++) { int index = (int) (Long.valueOf(&quot;0000003D&quot;, 16) &amp; idx); outChars.append(DIGITS&#x5B;index]); idx = idx &gt;&gt; 5;\r\n            }\r\n            shortStr&#x5B;i] = outChars.toString();\r\n        }\r\n        return shortStr;\r\n    }\r\n\r\n}\r\n\r\n<\/pre>\n<p>\u9879\u76ee\u4e2d\u6309\u7167\u957f\u94fe\u751f\u6210\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u4e3a6\u4f4d\u7684\u77ed\u94fe\u63a5\uff0c\u53ea\u8981\u957f\u94fe\u662f\u4e00\u6837\u7684\uff0c\u90a3\u4e48\u6bcf\u6b21\u751f\u6210\u7684\u77ed\u94fe\u4e5f\u4e00\u6837\u3002<\/p>\n<p>\u8003\u8651\u5230\u6bcf\u6b21\u751f\u6210\u77ed\u94fe\u524d\u8981\u67e5\u8be2\u4e00\u4e0b\u957f\u94fe\u662f\u4e0d\u662f\u5b58\u5728\uff0c\u7ee7\u7eed\u52a0\u4e00\u4e2a\u5c0f\u7f13\u5b58\uff0c\u8fd9\u6b21\u53cd\u8fc7\u6765\uff0c\u957f\u94fe\u4f5ckey\uff0c\u77ed\u94fe\u505avalue\u3002<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">redisTemplate.opsForHash().put(&quot;short-urls&quot;, url, slug);\r\n<\/pre>\n<p>\u5c3e\u58f0<br \/>\n\u7528\u6bd4\u8f83\u7b80\u5355\u7684\u65b9\u5f0f\u5b8c\u6210\u4e00\u4e2a\u77ed\u94fe\u63a5\u670d\u52a1\uff0c\u5728\u505a\u77ed\u94fe\u8df3\u8f6c\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u5206\u6790\u7528\u6237\u7684\u5404\u7c7b\u884c\u4e3a\uff0c\u6bd4\u5982\u64cd\u4f5c\u7cfb\u7edf\uff0c\u6d4f\u89c8\u5668\u7248\u672c\uff0c\u5730\u57df\u7b49\u7b49\u3002\u6700\u540e\u8bf4\u4e24\u70b9\u8981\u6ce8\u610f\u7684\uff1a<\/p>\n<p>redis\u6bd5\u7adf\u662f\u4f5c\u4e3a\u7f13\u5b58\u4f7f\u7528\u7684\uff0c\u5efa\u8bae\u6570\u636e\u8fd8\u662f\u8981\u843d\u4e00\u6b21\u5728DB\uff0c\u6bd4\u5982\u5728\u751f\u6210\u77ed\u94fe\u65f6\uff0c\u5199\u5165\u5230DB\u4e00\u4efd\u3002\u8fd9\u6837\u5f53redis\u6302\u4e86\uff0c\u8fd8\u53ef\u4ee5\u4eceDB\u4e2d\u5168\u91cf\u6062\u590d\u3002<br \/>\n\u5bf9\u4e8e\u957f\u65f6\u95f4\u6ca1\u6709PV\u7684\u77ed\u94fe\uff0c\u6bd4\u5982\u8d85\u8fc71\u5e74\u6ca1\u6709PV\uff0c\u9700\u8981\u505a\u6279\u91cf\u6e05\u7406\uff0c\u4e00\u822c\u4e00\u4e2a\u6708\u4e00\u6b21\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u5f88\u4e45\u6ca1\u66f4\u65b0\u4e86\uff0c\u52a0\u5165\u4e86\u521b\u4e1a\u516c\u53f8\uff0c\u4ece\u65e0\u5230\u6709\u7684\u6784\u5efa\u4e00\u5957\u4ea7\u54c1\uff0c\u4ece\u4e00\u5f00\u59cb\u7684\u7801\u6846\u67b6\u5230\u73b0\u5728\u601d\u8003\u5982\u4f55\u4f18\u5316\u4ea7\u54c1\u67b6\u6784\uff0c\u9002\u5e94\u66f4\u591a\u7684\u5f39\u6027\u9700\u6c42\u548c\u66f4\u5927\u7684\u6570\u636e\u91cf\u3002\u65f6\u95f4\u771f\u7684\u662f\u4e0d\u591f\u7528\uff0c\u51e0\u4e4e\u5feb\u534a\u5e74\u6ca1\u6709\u66f4\u65b0\u535a\u5ba2\u3002\u8fd1\u671f\u4ea7\u54c1\u6162\u6162\u8d70\u4e0a\u6b63\u8f68\uff0c\u53c8\u6709\u5c0f\u4f19\u4f34\u52a0\u5165\uff0c\u6240\u4ee5\u62bd\u51fa\u70b9\u65f6\u95f4\u603b\u7ed3\u4e00\u4e0b\u8fd9\u5c0f\u534a &#8230;<\/p>\n","protected":false},"author":1,"featured_media":1177,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[422,423],"class_list":["post-1176","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-development-of-communication-technology","tag-java","tag-423"],"post_thumbnail_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png?imageView2\/1\/w\/300\/h\/300#","content_first_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","post_medium_image_300":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png?imageView2\/1\/w\/600\/h\/600#","post_thumbnail_image_624":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","post_frist_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","post_medium_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png?imageView2\/1\/w\/600\/h\/600#","post_large_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","post_full_image":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","post_all_images":[{"imagesurl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2019\/04\/img_5cbfe10b8a8de.png","id":"image0"}],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":5,"total_comments":0,"category_name":"\u5f00\u53d1\u6280\u672f\u4ea4\u6d41","post_date":"2019-04-24","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"http:\/\/images.jiandaima.com\/blog\/wp-content\/uploads\/2020\/02\/img_5e44112722964.png","avatarurls":[],"related_posts":[],"pageviews":1491,"next_post_id":1286,"next_post_title":"\u5c0f\u7a0b\u5e8f\u641c\u7d22\u4f18\u5316\u6307\u5357(SEO)","previous_post_id":1173,"previous_post_title":"java\u6bd5\u4e1a\u8bbe\u8ba1\u5b66\u4e60_\u5b9a\u505a_\u8ba1\u7b97\u673a\u6bd5\u4e1a\u8bba\u6587\u4e0b\u8f7d\u5e73\u53f0","_links":{"self":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/1176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":0,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/media\/1177"}],"wp:attachment":[{"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jiandaima.com\/blog\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}