{"id":470,"date":"2017-02-08T21:31:27","date_gmt":"2017-02-08T13:31:27","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=470"},"modified":"2023-08-07T20:57:29","modified_gmt":"2023-08-07T12:57:29","slug":"java-article40","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/02\/08\/java-article40\/","title":{"rendered":"\u4f7f\u7528Java\u5bf9UTF8URL\u8fdb\u884c\u7f16\u7801\u89e3\u7801\u4ee5\u53ca\u8fdb\u884c\u7f16\u7801\u65b9\u5f0f\u7684\u5224\u65ad"},"content":{"rendered":"<h2>\u5728\u7528\u641c\u7d22\u5f15\u64ce\u641c\u7d22\u65f6\uff0c\u6240\u8f93\u5165\u7684\u5b57\u7b26\u901a\u5e38\u4f1a\u88ab\u7f16\u7801\u7136\u540e\u518d\u67e5\u8be2\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c06Java\u4e2d\u5bf9UTF8\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\u7684\u68c0\u6d4b\uff0cUTF8URL\u7f16\u7801\u89e3\u7801\u7684\u4ee3\u7801\u6302\u4e86\u51fa\u6765\uff0c\u9700\u8981\u65f6\u6539\u53d8main\u51fd\u6570\u4e2d\u7684url\u7136\u540e\u76f4\u63a5\u8fd0\u884c\u5373\u53ef\u3002<\/h2>\n<pre><code class=\"Java\">package com.rain.demo;\nimport java.io.UnsupportedEncodingException;\npublic class UTF8{\n  \/**\n   * Utf8URL\u7f16\u7801\n   * @param s\n   * @return\n   *\/\n  public static final String Utf8URLencode(String text) {\n    StringBuffer result = new StringBuffer();\n    for (int i = 0; i &lt; text.length(); i++) {\n      char c = text.charAt(i);\n      if (c &gt;= 0 &amp;&amp; c &lt;= 255) {\n        result.append(c);\n      }else {\n        byte[] b = new byte[0];\n        try {\n          b = Character.toString(c).getBytes(\"UTF-8\");\n        }catch (Exception ex) {\n        }\n        for (int j = 0; j &lt; b.length; j++) {\n          int k = b[j];\n          if (k &lt; 0) k += 256;\n          result.append(\"%\" + Integer.toHexString(k).toUpperCase());\n        }\n      }\n    }\n    return result.toString();\n  }\n  \/**\n   * Utf8URL\u89e3\u7801\n   * @param text\n   * @return\n   *\/\n  public static final String Utf8URLdecode(String text) {\n    String result = \"\";\n    int p = 0;\n    if (text!=null &amp;&amp; text.length()&gt;0){\n      text = text.toLowerCase();\n      p = text.indexOf(\"%e\");\n      if (p == -1) return text;\n      while (p != -1) {\n        result += text.substring(0, p);\n        text = text.substring(p, text.length());\n        if (text == \"\" || text.length() &lt; 9) return result;\n\n        result += CodeToWord(text.substring(0, 9));\n        text = text.substring(9, text.length());\n        p = text.indexOf(\"%e\");\n      }\n    }\n    return result + text;\n  }\n  \/**\n   * utf8URL\u7f16\u7801\u8f6c\u5b57\u7b26\n   * @param text\n   * @return\n   *\/\n  private static final String CodeToWord(String text) {\n    String result;\n    if (Utf8codeCheck(text)) {\n      byte[] code = new byte[3];\n      code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);\n      code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);\n      code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);\n      try {\n        result = new String(code, \"UTF-8\");\n      }catch (UnsupportedEncodingException ex) {\n        result = null;\n      }\n    }\n    else {\n      result = text;\n    }\n    return result;\n  }\n  \/**\n   * \u7f16\u7801\u662f\u5426\u6709\u6548\n   * @param text\n   * @return\n   *\/\n  private static final boolean Utf8codeCheck(String text){\n    String sign = \"\";\n    if (text.startsWith(\"%e\"))\n      for (int i = 0, p = 0; p != -1; i++) {\n        p = text.indexOf(\"%\", p);\n        if (p != -1)\n          p++;\n        sign += p;\n      }\n    return sign.equals(\"147-1\");\n  }\n  \/**\n   * \u5224\u65ad\u662f\u5426Utf8Url\u7f16\u7801\n   * @param text\n   * @return\n   *\/\n  public static final boolean isUtf8Url(String text) {\n    text = text.toLowerCase();\n    int p = text.indexOf(\"%\");\n    if (p != -1 &amp;&amp; text.length() - p &gt; 9) {\n      text = text.substring(p, p + 9);\n    }\n    return Utf8codeCheck(text);\n  }\n  \/**\n   * \u6d4b\u8bd5\n   * @param args\n   *\/\n  public static void main(String[] args) {\n    String url;\n    url = \"http:\/\/www.google.com\/search?hl=zh-CN&amp;newwindow=1&amp;q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5%9C%A8%E7%BA%BF%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2&amp;btnG=%E6%90%9C%E7%B4%A2&amp;lr=\";\n    if(CharTools.isUtf8Url(url)){\n      System.out.println(CharTools.Utf8URLdecode(url));\n    }\n    url = \"http:\/\/www.google.com\/search?hl=zh-cn&amp;newwindow=1&amp;q=\u4e2d\u56fd\u5927\u767e\u79d1\u5728\u7ebf\u5168\u6587\u68c0\u7d22&amp;btng=\u641c\u7d22&amp;lr=\";\n    if(!CharTools.isUtf8Url(url)){\n        System.out.println(CharTools.Utf8URLencode(url));\n    }\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7528\u641c\u7d22\u5f15\u64ce\u641c\u7d22\u65f6\uff0c\u6240\u8f93\u5165\u7684\u5b57\u7b26\u901a\u5e38\u4f1a\u88ab\u7f16\u7801\u7136\u540e\u518d\u67e5\u8be2\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5c06Java\u4e2d\u5bf9UTF8\u5b57\u7b26\u7f16\u7801\u65b9\u5f0f\u7684\u68c0\u6d4b\uff0cUT\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/02\/08\/java-article40\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":224,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[31],"tags":[46,85],"class_list":["post-470","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-java","tag-java","tag-code","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":1500,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/470\/revisions\/1500"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/224"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}