Emoji are not being encoded correctly for output writer

谁说胖子不能爱 提交于 2019-12-10 18:09:40

问题


The program takes in a comment and persists it. The database stores the value correctly.( i'm able to copy and paste it into an emoji page and it shows up correctly). The string i see in the debugger on the postComment request and in the getAllComments response is the same, but it is sending {0xED, 0xA0, 0xBD, 0xED, 0xB8, 0x80 } instead of { 0xF0, 0x9F, 0x98, 0x80 } and showing up as several characters instead of 1. If i set the encoding to UnicodeBig the emoji show up in the response, but we need to be using UTF-8

String jsonString = jsonMapper.toJson(jsonResponse);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().println(jsonString);

Do i need to do something to these strings before having the system encode to utf8? the libs used are

json-simple-1.1(current is 1.1.1) jackson-core-2.2.3(current 2.6)

Thank you.


回答1:


This ended up being an issue with Jetty 7. to get around this you can just .getBytes(String) and then write the bytes out and flush the buffer. Java will get the correct bytes and not encode the surrogate pair which is what Jetty was doing by default.



来源:https://stackoverflow.com/questions/30879346/emoji-are-not-being-encoded-correctly-for-output-writer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!