我JAVA基础不是很牢,URLDecoder函数没用过,不知道怎么用,但我自己编了个解码的
可以把从网页传过来的,正确显示汉字,希望对你有用
代码:
public String doEncoding(String code)
throws ServletException,IOException,DocumentException,UnsupportedEncodingException{
logger.info("EncodeToUTF.doEncoding is begin...");
String str = new String(code.getBytes("ISO-8859-1"),"UTF-8");
logger.debug("str="+str+";str.length="+str.length());
byte[] abc = str.getBytes("UTF-8");
StringBuffer strbuf = new StringBuffer();
System.out.println("abc.length"+abc.length);
for(int i=0;i<abc.length;i++){
//查看UTF-8编码,通过UTF-8判断是汉字还是英文和数字
logger.debug("abc["+i+"]="+abc[i]);
if(abc[i]<0){
String hex = Integer.toHexString((int)abc[i]);
strbuf.append("%").append(hex.substring(6));
}else if(abc[i]>0){
strbuf.append(code.charAt(i));
}
}
logger.info("strbuf="+strbuf.toString());
return strbuf.toString();
}