'How to solve Chinese garbled when using javax.servlet.Filter
ContentCachingResponseWrapper wrapper = new ContentCachingResponseWrapper(response);
wrapper.setContentType("application/json;charset=UTF-8");
wrapper.setCharacterEncoding("UTF-8");
filterChain.doFilter(request, wrapper);
if (uris != null) {
byte[] bytes = wrapper.getContentAsByteArray();
String content = new String(bytes, StandardCharsets.UTF_8);
Object jsonObject = JSONObject.parse(content);
if (jsonObject != null) {
for (int i = 0; i < uris.size(); i++) {
String[] attrs = uris.get(i).split("\\.");
if (!rights.contains(attrs[attrs.length-1])){
filter(jsonObject, attrs, 0);
}
}
wrapper.reset();
wrapper.getWriter().write(((JSONObject) jsonObject).toJSONString());
}
}
wrapper.copyBodyToResponse();
**Why response to the web page ,the Chinese characters become ???? And If I remove this filter, the Chinese characters become normal. **
Solution 1:[1]
The wrapper.reset() method will reset characterEncoding of wrapper to ISO-8859-1 ,So I use wrapper.setCharacterEncoding(StandardCharsets.UTF_8.toString()); wrapper.resetBuffer(); Then solved this bug.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | qun cheng |