weixin_36165756
weixin_36165756
采纳率0%
2018-10-23 02:57 阅读 2.3k

调用别人的rest接口,本地测试返回不乱码,运行在tomcat上会出现部分中文乱码,跪求大神

private ResResult getResResult(HttpURLConnection httpURLConnection, String policyStatusJson)
{
BufferedReader bufferedReader = null;
DataOutputStream dataOutputStream = null;
ResResult res = null;
try
{
if (policyStatusJson != null)
{
dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(policyStatusJson.getBytes());
}
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuffer resultBuf = new StringBuffer("");
String line = null;
while ((line = bufferedReader.readLine()) != null) {
line = new String(line.getBytes(), "utf-8");
resultBuf.append(line);
}
res = (ResResult)new Gson().fromJson(resultBuf.toString(), ResResult.class);
httpURLConnection.disconnect();

  return res;
}
catch (IOException e)
{
  e.printStackTrace();
}
finally
{
  try
  {
    if (bufferedReader != null) {
      bufferedReader.close();
    }
    if (dataOutputStream != null) {
      dataOutputStream.close();
    }
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
}
return res;

}

==========本地测试结果============

最新更新时间:2018-10-15 16:48:29.31 内容是:保单状态1

===========tomcat服务运行结果==========

:�?新更新时间:2018-10-15 16:48:29.31 内容是:保单状�??1;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • crazydddfz crazydddfz 2018-10-23 03:00
  • dltsbydh 紫气天堂 2018-10-23 05:57

    HttpURLConnection设置Content-Type了吗

    点赞 评论 复制链接分享
  • chen24yongcsdn chen24yongcsdn 2018-10-23 08:24

    tomcat 配置文件中可以配置编码 去conf中配置即可

    点赞 评论 复制链接分享
  • Caremorelei 忧忧忧雨 2018-10-23 08:51

    很明显的 直接找TOMCAT 啊~~~~~~

    点赞 评论 复制链接分享
  • yang1213952206 緣在何處 2018-10-24 01:23

    数据库中配置utf-8了吗?

    点赞 评论 复制链接分享
  • u013193464 此白手 2018-10-25 06:55

    tomcat server.xml的编码格式配置了吗 URIEncoding="UTF-8"

    点赞 评论 复制链接分享