URLEncoding
URLEncoding是用于解決鏈接字符串中包含中文字符的一種轉換編碼。各種編程環境下幾乎帶有它的庫函數。
不過,J2ME除外。
好在JAVA的源代碼中帶有這個類,我們把它拷貝到J2ME環境下編譯到我們的應用當中就可以了。
該文件位于JDK的目錄下src.zip文件中,名叫URLEncoder.java。
但是,這個文件還需要做很多修改才能使用在J2ME環境中。
先警告大家,有幾個真機(其中一個就是索愛的,好像是k500c),不管輸入什么樣的Encodeing都會出錯,甚至是“UTF-8”。所以我一怒之下 去除了Encodeing參數。(這可是在實際應用中得出的結論,不去掉的話可以在大部分情況下正常使用,但是,現實總是有點缺陷)
修改后的代碼如下,大家請放心使用。如果有興趣,可以比較兩個代碼,看看我改動了什么地方。
不過,J2ME除外。
好在JAVA的源代碼中帶有這個類,我們把它拷貝到J2ME環境下編譯到我們的應用當中就可以了。
該文件位于JDK的目錄下src.zip文件中,名叫URLEncoder.java。
但是,這個文件還需要做很多修改才能使用在J2ME環境中。
先警告大家,有幾個真機(其中一個就是索愛的,好像是k500c),不管輸入什么樣的Encodeing都會出錯,甚至是“UTF-8”。所以我一怒之下 去除了Encodeing參數。(這可是在實際應用中得出的結論,不去掉的話可以在大部分情況下正常使用,但是,現實總是有點缺陷)
修改后的代碼如下,大家請放心使用。如果有興趣,可以比較兩個代碼,看看我改動了什么地方。
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
public class HURLEncoder {
private static boolean[] dontNeedEncoding;
static {
dontNeedEncoding = new boolean[256];
for (int i = 0; i < 256; i++) {
boolean b = ((i >= ‘0′) && (i <= ‘9′))
|| ((i >= ‘A’) && (i <= ‘Z’)) || ((i >= ‘a’) && (i <= ‘z’));
dontNeedEncoding[i] = b;
}
dontNeedEncoding[’ ‘] = true;
dontNeedEncoding[’-'] = true;
dontNeedEncoding[’_'] = true;
dontNeedEncoding[’.'] = true;
dontNeedEncoding[’*'] = true;
}
public static String encode(String s) {
boolean wroteUnencodedChar = false;
StringBuffer writer = new StringBuffer();
StringBuffer out = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c < 256) && dontNeedEncoding[c]) {
if (c == ‘ ‘) {
c = ‘+’;
}
out.append((char) c);
wroteUnencodedChar = true;
} else {
try {
if (wroteUnencodedChar) {
writer = new StringBuffer();
wroteUnencodedChar = false;
}
writer.append(c);
if (c >= 0xD800 && c <= 0xDBFF) {
if ((i + 1) < s.length()) {
int d = (int) (s.charAt(i + 1));
if (d >= 0xDC00 && d <= 0xDFFF) {
writer.append(d);
i++;
}
}
}
} catch (Exception e) {
writer = new StringBuffer();
continue;
}
String str = writer.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeUTF(str);
dos.flush();
} catch (Exception e) {
e.printStackTrace();
}
byte[] temp = baos.toByteArray();
byte[] ba = new byte[temp.length - 2];
for (int ix = 0; ix < ba.length; ix++) {
ba[ix] = temp[ix + 2];
}
for (int j = 0; j < ba.length; j++) {
out.append(’%');
char ch = forDigit((ba[j] >> 4) & 0xF, 16);
out.append(ch);
ch = forDigit(ba[j] & 0xF, 16);
out.append(ch);
}
writer = new StringBuffer();
try {
dos.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return out.toString();
}
private static char forDigit(int digit, int radix) {
if ((digit >= radix) || (digit < 0)) {
return ‘0′;
}
if (digit < 10) {
return (char) (’0′ + digit);
}
return (char) (’A’ + digit - 10);
}
}
import java.io.DataOutputStream;
public class HURLEncoder {
private static boolean[] dontNeedEncoding;
static {
dontNeedEncoding = new boolean[256];
for (int i = 0; i < 256; i++) {
boolean b = ((i >= ‘0′) && (i <= ‘9′))
|| ((i >= ‘A’) && (i <= ‘Z’)) || ((i >= ‘a’) && (i <= ‘z’));
dontNeedEncoding[i] = b;
}
dontNeedEncoding[’ ‘] = true;
dontNeedEncoding[’-'] = true;
dontNeedEncoding[’_'] = true;
dontNeedEncoding[’.'] = true;
dontNeedEncoding[’*'] = true;
}
public static String encode(String s) {
boolean wroteUnencodedChar = false;
StringBuffer writer = new StringBuffer();
StringBuffer out = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ((c < 256) && dontNeedEncoding[c]) {
if (c == ‘ ‘) {
c = ‘+’;
}
out.append((char) c);
wroteUnencodedChar = true;
} else {
try {
if (wroteUnencodedChar) {
writer = new StringBuffer();
wroteUnencodedChar = false;
}
writer.append(c);
if (c >= 0xD800 && c <= 0xDBFF) {
if ((i + 1) < s.length()) {
int d = (int) (s.charAt(i + 1));
if (d >= 0xDC00 && d <= 0xDFFF) {
writer.append(d);
i++;
}
}
}
} catch (Exception e) {
writer = new StringBuffer();
continue;
}
String str = writer.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
try {
dos.writeUTF(str);
dos.flush();
} catch (Exception e) {
e.printStackTrace();
}
byte[] temp = baos.toByteArray();
byte[] ba = new byte[temp.length - 2];
for (int ix = 0; ix < ba.length; ix++) {
ba[ix] = temp[ix + 2];
}
for (int j = 0; j < ba.length; j++) {
out.append(’%');
char ch = forDigit((ba[j] >> 4) & 0xF, 16);
out.append(ch);
ch = forDigit(ba[j] & 0xF, 16);
out.append(ch);
}
writer = new StringBuffer();
try {
dos.close();
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return out.toString();
}
private static char forDigit(int digit, int radix) {
if ((digit >= radix) || (digit < 0)) {
return ‘0′;
}
if (digit < 10) {
return (char) (’0′ + digit);
}
return (char) (’A’ + digit - 10);
}
}
posted on 2008-08-15 14:49 LukeW 閱讀(1507) 評論(0) 編輯 收藏 所屬分類: Tips, Tricks, Hints & Code