1. package com.gootrip.util;
  2. import java.io.UnsupportedEncodingException;
  3. /**
  4. * <p>Title:字符编码工具类 </p>
  5. * <p>Description: </p>
  6. * <p>Copyright: Copyright (c) 2007</p>
  7. * <p>Company: </p>
  8. * @author:
  9. * @version 1.0
  10. */
  11. public class CharTools {
  12. /**
  13. * 转换编码 ISO-8859-1到GB2312
  14. * @param text
  15. * @return
  16. */
  17. public static final String ISO2GB(String text) {
  18. String result = "";
  19. try {
  20. result = new String(text.getBytes("ISO-8859-1"), "GB2312");
  21. }
  22. catch (UnsupportedEncodingException ex) {
  23. result = ex.toString();
  24. }
  25. return result;
  26. }
  27. /**
  28. * 转换编码 GB2312到ISO-8859-1
  29. * @param text
  30. * @return
  31. */
  32. public static final String GB2ISO(String text) {
  33. String result = "";
  34. try {
  35. result = new String(text.getBytes("GB2312"), "ISO-8859-1");
  36. }
  37. catch (UnsupportedEncodingException ex) {
  38. ex.printStackTrace();
  39. }
  40. return result;
  41. }
  42. /**
  43. * Utf8URL编码
  44. * @param s
  45. * @return
  46. */
  47. public static final String Utf8URLencode(String text) {
  48. StringBuffer result = new StringBuffer();
  49. for (int i = 0; i < text.length(); i++) {
  50. char c = text.charAt(i);
  51. if (c >= 0 && c <= 255) {
  52. result.append(c);
  53. }else {
  54. byte[] b = new byte[0];
  55. try {
  56. b = Character.toString(c).getBytes("UTF-8");
  57. }catch (Exception ex) {
  58. }
  59. for (int j = 0; j < b.length; j++) {
  60. int k = b[j];
  61. if (k < 0) k += 256;
  62. result.append("%" + Integer.toHexString(k).toUpperCase());
  63. }
  64. }
  65. }
  66. return result.toString();
  67. }
  68. /**
  69. * Utf8URL解码
  70. * @param text
  71. * @return
  72. */
  73. public static final String Utf8URLdecode(String text) {
  74. String result = "";
  75. int p = 0;
  76. if (text!=null && text.length()>0){
  77. text = text.toLowerCase();
  78. p = text.indexOf("%e");
  79. if (p == -1) return text;
  80. while (p != -1) {
  81. result += text.substring(0, p);
  82. text = text.substring(p, text.length());
  83. if (text == "" || text.length() < 9) return result;
  84. result += CodeToWord(text.substring(0, 9));
  85. text = text.substring(9, text.length());
  86. p = text.indexOf("%e");
  87. }
  88. }
  89. return result + text;
  90. }
  91. /**
  92. * utf8URL编码转字符
  93. * @param text
  94. * @return
  95. */
  96. private static final String CodeToWord(String text) {
  97. String result;
  98. if (Utf8codeCheck(text)) {
  99. byte[] code = new byte[3];
  100. code[0] = (byte) (Integer.parseInt(text.substring(1, 3), 16) - 256);
  101. code[1] = (byte) (Integer.parseInt(text.substring(4, 6), 16) - 256);
  102. code[2] = (byte) (Integer.parseInt(text.substring(7, 9), 16) - 256);
  103. try {
  104. result = new String(code, "UTF-8");
  105. }catch (UnsupportedEncodingException ex) {
  106. result = null;
  107. }
  108. }
  109. else {
  110. result = text;
  111. }
  112. return result;
  113. }
  114. /**
  115. * 编码是否有效
  116. * @param text
  117. * @return
  118. */
  119. private static final boolean Utf8codeCheck(String text){
  120. String sign = "";
  121. if (text.startsWith("%e"))
  122. for (int i = 0, p = 0; p != -1; i++) {
  123. p = text.indexOf("%", p);
  124. if (p != -1)
  125. p++;
  126. sign += p;
  127. }
  128. return sign.equals("147-1");
  129. }
  130. /**
  131. * 判断是否Utf8Url编码
  132. * @param text
  133. * @return
  134. */
  135. public static final boolean isUtf8Url(String text) {
  136. text = text.toLowerCase();
  137. int p = text.indexOf("%");
  138. if (p != -1 && text.length() - p > 9) {
  139. text = text.substring(p, p + 9);
  140. }
  141. return Utf8codeCheck(text);
  142. }
  143. /**
  144. * 测试
  145. * @param args
  146. */
  147. public static void main(String[] args) {
  148. //CharTools charTools = new CharTools();
  149. String url;
  150. 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=";
  151. if(CharTools.isUtf8Url(url)){
  152. System.out.println(CharTools.Utf8URLdecode(url));
  153. }else{
  154. //System.out.println(URLDecoder.decode(url));
  155. }
  156. 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";
  157. if(CharTools.isUtf8Url(url)){
  158. System.out.println(CharTools.Utf8URLdecode(url));
  159. }else{
  160. //System.out.println(URLDecoder.decode(url));
  161. }
  162. }
  163. }

Java-CharTools工具类的更多相关文章

  1. Java Properties工具类详解

    1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...

  2. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  3. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  4. Java并发工具类 - CountDownLatch

    Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...

  5. MinerUtil.java 爬虫工具类

    MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...

  6. MinerDB.java 数据库工具类

    MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...

  7. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  8. Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie

    Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...

  9. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  10. java日期工具类DateUtil-续一

    上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...

随机推荐

  1. SQL用法三(游标和Fetch)

    /一般情况下,我们用SELECT这些查询语句时,都是针对的一行记录而言,如果要在查询分析器中对多行记录(即记录集)进行读取操作时,则需要使用到游标或WHILE等循环/以下内容摘自http://www. ...

  2. 大数据备忘录———将数据从oracle导入impala中

    上周遇到了将数据从oracle导入到impala的问题,这个项目耽误了我近一周的时间,虽然是种种原因导致的,但是还是做个总结. 需求首先是跑数据,跑数据这个就不叙述,用的是公司的平台. 讲讲耽误我最久 ...

  3. LC 155 Min Stack

    问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

  4. # MATLAB数据处理

    目录 MATLAB数据处理 数据归一化处理 冒号的作用(获取指定行列的数据) MATLAB数据处理 mean(A,(b)) %均值函数,b为设置对哪一维上的数据进行处理,默认为第一维(列),行为第二维 ...

  5. asp.net练习②——Paginaton无刷新分页

    aspx代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server" ...

  6. 美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)

    大意: 给定树, 初始每个点全为白色, 点$i$有权值$k_i$, 表示选择$i$后, 所有距离$i$小于$k_i$的祖先(包括i)会变为黑色, 求最少选多少个点能使所有点变为黑色. 链上情况的话, ...

  7. Array Product CodeForces - 1042C (细节)

    #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> ...

  8. 终身机器学习(Lifelong Machine Learning)综述

    终身机器学习(Lifelong Machine Learning)综述 2015年10月23日 17:34:57 qrlhl 阅读数 7805更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  9. servlet容器,web容器,spring容器,springmvc容器的区别(转)

    web容器中有servlet容器,spring项目部署后存在spring容器和springmvc容器.其中spring控制service层和dao层的bean对象.springmvc容器控制contr ...

  10. golang(10):web开发 & 连接数据库

    http编程 ) Go原生支持 http : import ("net/http") ) Go 的 http 服务性能和 nginx 比较接近 ) 几行代码就可以实现一个 web ...