jsp页面提交的参数到达Tomcat后台后显示为乱码,

2025-03-28 03:53:29
推荐回答(2个)
回答1:

肯定是GBK和UTF-8的转码了,用
TransFormat.GBToUnicode试试

附上源码
import java.io.UnsupportedEncodingException;

public class TransFormat
{

public TransFormat()
{
}

public static String unicodeToGB(String strIn)
{
String strOut = null;
if(strIn == null || strIn.trim().equals(""))
return strIn;
try
{
byte b[] = strIn.getBytes("GBK");
strOut = new String(b, "ISO8859_1");
}
catch(UnsupportedEncodingException unsupportedencodingexception) { }
return strOut;
}

public static String GBToUnicode(String strIn)
{
String strOut = null;
if(strIn == null || strIn.trim().equals(""))
return strIn;
try
{
byte b[] = strIn.getBytes("ISO8859_1");
strOut = new String(b, "GBK");
}
catch(Exception exception) { }
return strOut;
}

public static int GBlength(String strIn)
{
int len = 0;
String strOut = null;
if(strIn == null || strIn.trim().equals(""))
return 0;
try
{
byte b[] = strIn.getBytes("GBK");
len = b.length;
}
catch(Exception exception) { }
return len;
}
}

回答2:

把你的java类设置成utf-8或者GB2312的,在mysql里也要改一下