首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

流传递乱码有关问题

2012-01-15 
流传递乱码问题发送端String strURLhttp://localhost:8080/JavaNet/servlet/TestUrlConnectResponseUR

流传递乱码问题
发送端
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();

热点排行