流传递乱码问题
发送端
String strURL="http://localhost:8080/JavaNet/servlet/TestUrlConnectResponse";
URL url=new URL(strURL);
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
sb.append("<Root>\r\n");
sb.append("<Insurance>\r\n");
sb.append("<test>"+"一个"+"</test>\r\n");
sb.append("</Insurance>\r\n");
sb.append("</Root>\r\n");
try {
connect(url,sb.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void connect(URL url, String str) throws
IOException, Exception {
URLConnection connection = url.openConnection();
connection.setRequestProperty("accept-language", "zh-cn");
connection.setDoOutput(true);
OutputStream outs = connection.getOutputStream();
//将该输出流转换为对象输出流
ObjectOutputStream objout = new ObjectOutputStream(outs);
//将要传输的对象写入该对象输出流中
objout.writeObject(str);
objout.close();
String returnData="";
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((returnData=br.readLine())!=null){
System.out.println("客户端接收===="+returnData);
}
}
接收
String str="ok";
String str1="";
InputStream inputStream=request.getInputStream();
BufferedReader buffer=new BufferedReader(new InputStreamReader(inputStream));
while((str1=buffer.readLine())!=null){
System.out.println("服务器端接收===="+changeEncoding(str1));
} PrintWriter out = response.getWriter();
out.write(str);
out.close();
最后在服务端打印出
的XML都是乱码,对流没什么研究,希望大家指点指点,顺便帮我讲下写流的传递应该注意什么
------解决方案--------------------还是要自己解决,
BufferedWriter broutput = new BufferedWriter(new OutputStreamWriter(
connection.getOutputStream()));
broutput.write(str, 0, str.length());
broutput.close();
String returnData = "";
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ( (returnData = br.readLine()) != null) {
System.out.println("客户端接收====" + returnData);
}
is.close();
br.close();