欢迎光临
我们一直在努力

java socket 中文乱码_java-Socket接受中文乱码的解决 | 学步园

服务器发送一条数据如:

Java代码

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintWriter out = new PrintWriter(client.getOutputStream());

out.println("没有查到该数据。");//向客户端发送

out.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintWriter out = new PrintWriter(client.getOutputStream());

out.println("没有查到该数据。");//向客户端发送

out.flush();

然后客户端接收:

Java代码

public BufferedReader recieve(){

//从服务端程序接收数据

InputStreamReader isr=null;

BufferedReader br=null;

try {

isr = new InputStreamReader(server.getInputStream());//这里需要GBK

br = new BufferedReader(isr);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return br;

}

///

BufferedReader br=recieve();

while((message = br.readLine()) != null){

messages+=message;

}

System.out.println("messages="+messages);

public BufferedReader recieve(){

//从服务端程序接收数据

InputStreamReader isr=null;

BufferedReader br=null;

try {

isr = new InputStreamReader(server.getInputStream());//这里需要GBK

br = new BufferedReader(isr);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return br;

}

///

BufferedReader br=recieve();

while((message = br.readLine()) != null){

messages+=message;

}

System.out.println("messages="+messages);

你会发现这样写会是乱码;

应该写成isr = new InputStreamReader(server.getInputStream(),"GBK");

这样就正常显示了。

在服务端默认的编码情况下,JAVA的SOCKET接收需要GBK编码,而C#的接收需要UTF-8编码

赞(0) 打赏
未经允许不得转载:AFT博客 » java socket 中文乱码_java-Socket接受中文乱码的解决 | 学步园

登录

找回密码

注册