The charset of the form page is ISO-8859-15'. HTTP will encode the form message in URLEncoding, that is, space is replaced with +’; non-ASCII characters are encoded inthe format %XX', where XX’ stands for the hexadecimal value of the character.
The URL decoder in this system looks as follows:
importjava.io.UnsupportedEncodingException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* UrlDecoder decodes URL encoded information into quoted printable characters.
*
* @author Daoyuan Li
*/
public class UrlDecoder {
/**
* Decode from URL encoding to quoted printable.
* @param url URL encoded message to be decoded.
* @return Decoded quoted printable string.
* @throwsException If anything wrong happens, an Exception is thrown.
*/
public String decode(String url) throws Exception {
String decoded = "";
Exception ex = new Exception();
Vector buf = new Vector();
for (int i = 0; i < url.length(); i++) {
if (url.charAt(i) == '%') {
if (i + 2 >= url.length()) {
throw ex;
}
int d = -1;
try {
d = Integer.parseInt(url.substring(i + 1, i + 3), 16);
} catch (NumberFormatException e) {
throw ex;
}
if (d > 255 || d < 0) {
throw ex;
}
buf.add(new Byte((byte)d));
i += 2;
} else if (url.charAt(i) == '+') {
buf.add(new Byte((byte)' '));
} else {
buf.add(new Byte((byte) url.charAt(i)));
}
}
try {
byte[] dcd = new byte[buf.size()];
for (int j = 0; j < dcd.length; j++) {
dcd[j] = buf.elementAt(j);
}
decoded = new String(dcd, "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
Logger.getLogger(FormValidator.class.getName()).log(Level.SEVERE, null,e);
throw ex;
}
return decoded;
}
}
Leave a Comment










