url轉換工具
1
import java.io.UnsupportedEncodingException;
2
import java.net.URLDecoder;
3
4
public 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
使用自然會明白。

2

3

4



5

6


7

8

9

10

11

12



13

14



15

16



17

18

19

20

21

22


23

24

25

26

27

28



29

30



31

32



33

34

35

36

37

38


39

40

41

42

43

44



45

46

47



48

49

50



51

52



53

54

55



56

57



58

59

60



61

62

63

64

65

66

67

68

69

70

71

72

73


74

75

76

77

78

79



80

81

82

83



84

85

86

87

88

89



90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105


106

107

108

109

110

111



112

113

114



115

116

117

118

119



120

121



122

123

124



125

126

127

128

129

130

131


132

133

134

135

136

137



138

139

140



141

142

143

144

145

146

147

148

149


150

151

152

153

154

155



156

157

158



159

160

161

162

163

164


165

166

167

168

169



170

171

172

173

174

175

176

177



178

179



180

181

182



183



184

185

186

187

188



189

190



191

192

193

194

195

196

197

posted on 2007-08-08 16:56 朱巖 閱讀(1867) 評論(1) 編輯 收藏 所屬分類: 中文亂碼問題