作者fantasywater (菸酒生)
看板java
标题[问题] server 与 client 传送讯息的问题
时间Tue Jun 15 21:05:41 2010
不好意思,想请教一下编码的问题:
我 Server 和 Client 传送中文或出现乱码,
我 client 是这样写的
------------------------------------------
String Send = "一二三";
byte[] bytes = Send.getBytes("UTF-8");
OutputStream out = conn.getOutputStream();
out.write(bytes);
------------------------------------------
Server 是这样接的
------------------------------------------
InputStream is = req.getInputStream();
byte[] bytes = new byte[50];
is.read(bytes);
String s = bytes.toString();
------------------------------------------
但是我的 s 最後会得到乱码,请问该如何更正呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.107.146
1F:推 willieliao:s = new String(bytes, "UTF-8"); 06/15 21:27
2F:推 PsMonkey:==.=== 你的 req 怎麽「建立」的? 06/15 21:44
3F:→ fantasywater:HttpServletRequest req 06/15 21:46
4F:→ fantasywater:一楼的方法好像不行QQ 06/15 21:57
5F:推 PsMonkey:先 req.setCharacter() 试试看吧..... 06/15 22:27
6F:→ fantasywater:我有设 可是无效QQ 06/15 22:42
7F:推 lubu:因为中文字是2bytes 但你存的矩阵是1byte 所以只存到一半吧 06/15 23:39
8F:→ sbrhsieh:问题在於 byte[] 的 toString method 不如你想像的 06/16 00:08
9F:推 PsMonkey:噗...都忘记看仔细了,可是他说一楼的方法无效 XD 06/16 00:34
10F:→ fantasywater:我解出来了啦 要用 getOutputStream() 操.. 06/16 00:44
11F:→ sbrhsieh:最好是这样~~~ 06/16 01:22
12F:→ fantasywater:string要改,但主要原因是其他某个部份的某一行~ 06/16 01:51