b*g
2 楼
这两天用到一个servlet, 需要处理中文的,所有的编码都用的utf8, 没什么问题,可是
今天没事play with it的时候,发现下面这一段:
String inputText = request.getParameter("input");
String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8")
;
myPrintWriter.println(modText);
基本上就是读入输入的中文,然后原样打印出来。但是我发现,其中第二句不管加上还是
去掉,在网页上显示的都是正确的中文。ISO-8859-1应该是只支持拉丁字母的
characterset, 所以inputText.getBytes("ISO-8859")这个操作应该会丢失inputText本
来来储存的中文信息,因为在8859-1的characterset里map不到,这样即使再用utf-8转回
来,也不应该是原来的信息了。可是为什么在我的test里面,不管有没有这句都没有区别
呢。bt
今天没事play with it的时候,发现下面这一段:
String inputText = request.getParameter("input");
String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8")
;
myPrintWriter.println(modText);
基本上就是读入输入的中文,然后原样打印出来。但是我发现,其中第二句不管加上还是
去掉,在网页上显示的都是正确的中文。ISO-8859-1应该是只支持拉丁字母的
characterset, 所以inputText.getBytes("ISO-8859")这个操作应该会丢失inputText本
来来储存的中文信息,因为在8859-1的characterset里map不到,这样即使再用utf-8转回
来,也不应该是原来的信息了。可是为什么在我的test里面,不管有没有这句都没有区别
呢。bt
t*5
3 楼
check javadoc of getBytes()
'the behavior is undefined' if string is beyond the charset.
the impl happens to decide just returning all the damn bytes.
是
")
是
本
回
【在 b***g 的大作中提到】
: 这两天用到一个servlet, 需要处理中文的,所有的编码都用的utf8, 没什么问题,可是
: 今天没事play with it的时候,发现下面这一段:
: String inputText = request.getParameter("input");
: String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8")
: ;
: myPrintWriter.println(modText);
: 基本上就是读入输入的中文,然后原样打印出来。但是我发现,其中第二句不管加上还是
: 去掉,在网页上显示的都是正确的中文。ISO-8859-1应该是只支持拉丁字母的
: characterset, 所以inputText.getBytes("ISO-8859")这个操作应该会丢失inputText本
: 来来储存的中文信息,因为在8859-1的characterset里map不到,这样即使再用utf-8转回
'the behavior is undefined' if string is beyond the charset.
the impl happens to decide just returning all the damn bytes.
是
")
是
本
回
【在 b***g 的大作中提到】
: 这两天用到一个servlet, 需要处理中文的,所有的编码都用的utf8, 没什么问题,可是
: 今天没事play with it的时候,发现下面这一段:
: String inputText = request.getParameter("input");
: String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8")
: ;
: myPrintWriter.println(modText);
: 基本上就是读入输入的中文,然后原样打印出来。但是我发现,其中第二句不管加上还是
: 去掉,在网页上显示的都是正确的中文。ISO-8859-1应该是只支持拉丁字母的
: characterset, 所以inputText.getBytes("ISO-8859")这个操作应该会丢失inputText本
: 来来储存的中文信息,因为在8859-1的characterset里map不到,这样即使再用utf-8转回
b*g
4 楼
Thanks for the help. 今天又作了一些测试,结果发现了更多的问题,原来这个
String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8");
是起作用的,如果不加这句,存进database就是乱码,加了这句,才能正常存储中文。
servlet 相关的命令如下:
request.setCharacterEncoding("UTF-8");
...
String inputText = request.getParameter("input");
String modText = new String(inputTextgetBytes("ISO-8859-1"), "UTF-8");
...
save to database
如果去掉转变编码的那一句,就不能正确存储. 可是我的jsp的content type 设的是
charset="UTF-8", request也设成了UTF-8, 为什么还需要用8859-1转化呢,真是百思不
得其解。难道是tomcat的bug?
")
是
本
回
String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8");
是起作用的,如果不加这句,存进database就是乱码,加了这句,才能正常存储中文。
servlet 相关的命令如下:
request.setCharacterEncoding("UTF-8");
...
String inputText = request.getParameter("input");
String modText = new String(inputTextgetBytes("ISO-8859-1"), "UTF-8");
...
save to database
如果去掉转变编码的那一句,就不能正确存储. 可是我的jsp的content type 设的是
charset="UTF-8", request也设成了UTF-8, 为什么还需要用8859-1转化呢,真是百思不
得其解。难道是tomcat的bug?
")
是
本
回
t*5
5 楼
in that case, my theory is that your browser sends in GB, and can
dispay GB, regardless of chartset specification.
you can not rely on a browser to check the bytes, look into HTTP
traffic directly to see what is sent between server and browser.
last time I know, it's a mess how browsers encode/escape request data.
lots of ambiguities, there is no way on server to interprete it
100% correctly. (yeah there are standards defining how it *shoud* be, but...
【在 b***g 的大作中提到】
: Thanks for the help. 今天又作了一些测试,结果发现了更多的问题,原来这个
: String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8");
: 是起作用的,如果不加这句,存进database就是乱码,加了这句,才能正常存储中文。
: servlet 相关的命令如下:
: request.setCharacterEncoding("UTF-8");
: ...
: String inputText = request.getParameter("input");
: String modText = new String(inputTextgetBytes("ISO-8859-1"), "UTF-8");
: ...
: save to database
dispay GB, regardless of chartset specification.
you can not rely on a browser to check the bytes, look into HTTP
traffic directly to see what is sent between server and browser.
last time I know, it's a mess how browsers encode/escape request data.
lots of ambiguities, there is no way on server to interprete it
100% correctly. (yeah there are standards defining how it *shoud* be, but...
【在 b***g 的大作中提到】
: Thanks for the help. 今天又作了一些测试,结果发现了更多的问题,原来这个
: String modText = new String(inputText.getBytes("ISO-8859-1"), "UTF-8");
: 是起作用的,如果不加这句,存进database就是乱码,加了这句,才能正常存储中文。
: servlet 相关的命令如下:
: request.setCharacterEncoding("UTF-8");
: ...
: String inputText = request.getParameter("input");
: String modText = new String(inputTextgetBytes("ISO-8859-1"), "UTF-8");
: ...
: save to database
相关阅读
java dumb questionRe: how to access the overrided fields oCan Java call C libraries?Java API for XML?Suggestion Re: 发现 synchronized 的一个问题问个关于jar的问题.Re: wrong!!! Re: 有没有 replaceall for Stri同学们,wrong!!! Re: 有没有 replaceall for String?HElP! ! 使用J2SDK1。4。2_04 出现的问题jdk 怎么装成了chinese的啊?GUI libraries for JDeveloper?help: 如何用servlet实现zip文件下载?问个java on linux的奇怪问题advice needed on workflow engineRe: RandomAccessFile和FileOutputStream有什么how do you debug java applet?change JSlider thumb icon?[转载] Problems with reading image file in Jar.问个Hashtable的问题