当前位置: 代码迷 >> 高性能WEB开发 >> http json往外输出乱码 ,报 CharConversionException,求大神搂一眼解决办法
  详细解决方案

http json往外输出乱码 ,报 CharConversionException,求大神搂一眼解决办法

热度:877   发布时间:2014-02-27 01:51:38.0
http json往外输出乱码 ,报 CharConversionException,求大神搂一眼
这个是一个http请求, 我把需要的东西弄成json.传个j2me写的nokia v5客户端. 传送代码:

public static void writeDataStream(HttpServletResponse response,String dataStr)throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
OutputStream os = null;
try {
dos.writeUTF(dataStr);
byte data [] = baos.toByteArray();
response.setCharacterEncoding("UTF-8");
response.setContentLength(data.length);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/octet-stream");
os = response.getOutputStream();
os.write(data);
os.flush();
dos.flush();
baos.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (os != null) {
os.close();
}
if (dos != null) {
dos.close();
}
if (baos != null) {
baos.close();
}
}
}

但是老会报出一个错误,客户端不受影响,能正常得到值, 因为是乱码.没法贴,只能给图.

实在不知道怎么出来的. 求大侠解释下这个怎么回事.还用说吗?肯定是编码啊,你确定一下你的字符串转成字节时用的是什么编码,再确定接收端是用什么编码.
  相关解决方案