简体   繁体   中英

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. **

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM