使用Java對UTF8URL進行編碼解碼以及進行編碼方式的判斷__java

來源:互聯網
上載者:User

更多技術文章請訪問我的個人部落格http://www.rain1024.com 在用搜尋引擎搜尋時,所輸入的字元通常會被編碼然後再查詢,這篇文章將Java中對UTF8字元編碼方式的檢測,UTF8URL編碼解碼的代碼掛了出來,需要時改變main函數中的url然後直接運行即可。

package com.rain.demo;import java.io.UnsupportedEncodingException;public class UTF8{  /**   * Utf8URL編碼   * @param s   * @return   */  public static final String Utf8URLencode(String text) {    StringBuffer result = new StringBuffer();    for (int i = 0; i < text.length(); i++) {      char c = text.charAt(i);      if (c >= 0 && c <= 255) {        result.append(c);      }else {        byte[] b = new byte[0];        try {          b = Character.toString(c).getBytes("UTF-8");        }catch (Exception ex) {        }        for (int j = 0; j < b.length; j++) {          int k = b[j];          if (k < 0) k += 256;          result.append("%" + Integer.toHexString(k).toUpperCase());        }      }    }    return result.toString();  }  /**   * Utf8URL解碼   * @param text   * @return   */  public static final String Utf8URLdecode(String text) {    String result = "";    int p = 0;    if (text!=null && text.length()>0){      text = text.toLowerCase();      p = text.indexOf("%e");      if (p == -1) return text;      while (p != -1) {        result += text.substring(0, p);        text = text.substring(p, text.length());        if (text == "" || text.length() < 9) return result;        result += CodeToWord(text.substring(0, 9));        text = text.substring(9, text.length());        p = text.indexOf("%e");      }    }    return result + text;  }  /**   * utf8URL編碼轉字元   * @param text   * @return   */  private static final String CodeToWord(String text) {    String result;    if (Utf8codeCheck(text)) {      byte[] code = new byte[3];      code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);      code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);      code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);      try {        result = new String(code, "UTF-8");      }catch (UnsupportedEncodingException ex) {        result = null;      }    }    else {      result = text;    }    return result;  }  /**   * 編碼是否有效   * @param text   * @return   */  private static final boolean Utf8codeCheck(String text){    String sign = "";    if (text.startsWith("%e"))      for (int i = 0, p = 0; p != -1; i++) {        p = text.indexOf("%", p);        if (p != -1)          p++;        sign += p;      }    return sign.equals("147-1");  }  /**   * 判斷是否Utf8Url編碼   * @param text   * @return   */  public static final boolean isUtf8Url(String text) {    text = text.toLowerCase();    int p = text.indexOf("%");    if (p != -1 && text.length() - p > 9) {      text = text.substring(p, p + 9);    }    return Utf8codeCheck(text);  }  /**   * 測試   * @param args   */  public static void main(String[] args) {    String url;    url = "http://www.google.com/search?hl=zh-CN&newwindow=1&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&btnG=%E6%90%9C%E7%B4%A2&lr=";    if(CharTools.isUtf8Url(url)){      System.out.println(CharTools.Utf8URLdecode(url));    }    url = "http://www.google.com/search?hl=zh-cn&newwindow=1&q=中國大百科線上全文檢索索引&btng=搜尋&lr=";    if(!CharTools.isUtf8Url(url)){        System.out.println(CharTools.Utf8URLencode(url));    }  }}

更多技術文章請訪問我的個人部落格http://www.rain1024.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.