打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java 字符编码工具类 [UTF-8 编码实现]

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URLDecoder;

/**
*

Title:字符编码工具类
*
Description:
*
Copyright: flashman.com.cn Copyright (c) 2005
*
Company: flashman.com.cn
* @author: jeffzhu
* @version 1.0
*/
public class CharTools {

  /**
   * 转换编码 ISO-8859-1到GB2312
   * @param text
   * @return
   */
  public String ISO2GB(String text) {
    String result = "";
    try {
      result = new String(text.getBytes("ISO-8859-1"), "GB2312");
    }
    catch (UnsupportedEncodingException ex) {
      result = ex.toString();
    }
    return result;
  }

  /**
   * 转换编码 GB2312到ISO-8859-1
   * @param text
   * @return
   */
  public String GB2ISO(String text) {
    String result = "";
    try {
      result = new String(text.getBytes("GB2312"), "ISO-8859-1");
    }
    catch (UnsupportedEncodingException ex) {
      ex.printStackTrace();
    }
    return result;
  }
  /**
   * Utf8URL编码
   * @param s
   * @return
   */
  public 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 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 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;
  }

  public static boolean isValidUtf8(byte[] b, int aMaxCount) {
    int lLen = b.length, lCharCount = 0;
    for (int i = 0; i < lLen && lCharCount < aMaxCount; ++lCharCount) {
      byte lByte = b[i++]; //to fast operation, ++ now, ready for the following for(;;)
      if (lByte >= 0) continue; //>=0 is normal ascii
      if (lByte < (byte) 0xc0 || lByte > (byte) 0xfd)
        return false;
      int lCount = lByte > (byte) 0xfc ? 5 : lByte > (byte) 0xf8 ? 4 : lByte > (byte) 0xf0 ? 3 : lByte > (byte) 0xe0 ? 2 : 1;
      if (i + lCount > lLen) return false;
      for (int j = 0; j < lCount; ++j, ++i)
        if (b[i] >= (byte) 0xc0)return false;
    }
    return true;
  }

  /**
   * 编码是否有效
   * @param text
   * @return
   */
  private 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 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) {

    CharTools charTools = new CharTools();

    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));
    }else{
      System.out.println(URLDecoder.decode(url));
    }

    url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg";
    if(charTools.isUtf8Url(url)){
      System.out.println(charTools.Utf8URLdecode(url));
    }else{
      System.out.println(URLDecoder.decode(url));
    }

  }

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#编程总结(十)字符转码
C# byte[]转string, string转byte[] 的四种方法
java中文乱码解决总结
Hadoop中文件读写(Java)
struts2中文乱码解决方法
CSDN 文档中心:手机中文码制问题的一点理解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服