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

qiunet 2013-07-04 10:09:28
这个是一个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();
}
}
}

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

实在不知道怎么出来的. 求大侠解释下这个怎么回事.
...全文
169 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiunet 2013-07-04
  • 打赏
  • 举报
回复
确定用的UTF-8,客户端也是getUTF(), 这个错误并不是每次都报,即使把原数据拿出来再请求一次,也没有办法模拟出来.
etnet 2013-07-04
  • 打赏
  • 举报
回复
还用说吗?肯定是编码啊,你确定一下你的字符串转成字节时用的是什么编码,再确定接收端是用什么编码.

25,980

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧