you must decode first.
use:byte[] in = strIn.getBytes("8859_1"); //8859_1 maybe others encode methods
then ,use the byte array to combine the new String
strOut = new String(in,"GB_2312");you can try it.
use:byte[] in = strIn.getBytes("8859_1"); //8859_1 maybe others encode methods
then ,use the byte array to combine the new String
strOut = new String(in,"GB_2312");you can try it.
though I have a Chinese Input Method ,
but , i am afraid that it can't be displayed correctly,
so , i write in english.
或
Stirng strOut = new String(strIn.getBytes("GBK"),"8859_1");
或
Stirng strOut = new String(strIn.getBytes(“8859_1"),"GBK");
或
Stirng strOut = new String(strIn.getBytes(),"GBK");
都试过了,都不行。但是我用jsp的时候,在网页上能显示中文,在applet上就显示不了中文,
会不会是jdk的问题。
在Applet下是乱码!
都是在同一台机器上的IE打开!
都是同一个数据库!
它怎么就这么衰呀!
上面给的解答都是关于在jsp与servlet中怎样解决java的中文问题。关于这个问题已经有许多资料。而提问者的意思是在applet中怎样正确显示中文,这与java的字符编码无关。
导致这一问题的原因在于你的java运行环境。问题的本质在于你的jre没有正确设置中文字符的“字形”显示(措词有些不当^_^)。也就是说,你的java已经正确的知道中文字符的编码,但是它不知道怎样显示它。
解决方案是:首先你要确保在你的jre\lib里面要有i18n.jar,然后还要有正确的font.properties文件。
仅给个提示(因为我现在实在是没时间去恢复以前的错误的jre环境,呵呵),你先看看吧。
祝你好运:)
以下是我用inet tds的jdbc驱动是试过,没有问题。
获取Connect连接的URL没有加chartset=gb2312的话:
try{
Stirng strOut = new String(strIn.getBytes(“ISO-8859-1"));
} catch)Exception ex){
ex.printStackTrace();
}
加了的话,就什么都不用做.
俺无聊之时上这来逛逛,顺便解决几个问题。
俺的答案真的是千真万确的,可怎么就没人吊俺呢?
真TMD的郁闷!
就象这个问题,让俺说几遍你才能懂?是你的JRE有问题!与你的字符编码无关!
你的java编码是正确的,只是它不知道怎样正确的显示它!
就好象你在word里面打汉字,想让它显示“NB楷体”字,可你的机器上没有“NB楷体”这种字体,你怎么让word正确显示出来?
俺一向是知书答礼,文质彬彬,可这次俺要发飙!
靠!再说的简单一些,你看看你的jre\版本号\lib里面有没有font.properties.zh.NT4.0这个文件,你复制一份,改成font.properties.zh.NT5.0先试试!
受不了了!
public static String AsciiToChineseString(String s) {
char[] orig = s.toCharArray();
byte[] dest = new byte[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (byte)(orig[i]&0xFF);
try {
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}
catch (Exception e) {
System.out.println(e);
return s;
}
} public static String ChineseStringToAscii(String s) {
try {
CharToByteConverter toByte = CharToByteConverter.getConverter("gb2312");
byte[] orig = toByte.convertAll(s.toCharArray());
char[] dest = new char[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (char)(orig[i] & 0xFF);
return new String(dest);
}
catch (Exception e) {
System.out.println(e);
return s;
}
}
用这两个函数试一下,很好用的。
我做的Applet-Servlet-SQL Server工作得很正常。
你去http://www.emapgo.com/main.html看看就知道了,界面上的四个Applet全部通过Servlet从数据库读取数据,包括文字和图像。:p(有打广告之嫌)
char[] orig = s.toCharArray();
byte[] dest = new byte[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (byte)(orig[i]&0xFF);
try {
ByteToCharConverter toChar = ByteToCharConverter.getConverter("gb2312");
return new String(toChar.convertAll(dest));
}
catch (Exception e) {
System.out.println(e);
return s;
}
} public static String ChineseStringToAscii(String s) {
try {
CharToByteConverter toByte = CharToByteConverter.getConverter"gb2312");
byte[] orig = toByte.convertAll(s.toCharArray());
char[] dest = new char[orig.length];
for (int i=0;i<orig.length;i++)
dest[i] = (char)(orig[i] & 0xFF);
return new String(dest);
}
catch (Exception e) {
System.out.println(e);
return s;
}
}
用这个试试^_^