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