【发布时间】:2011-07-23 14:45:42
【问题描述】:
stackoverflow.com 上的编码
{|}~€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþŸ▲►▼◄■□▣▤▥▦▧▨▩▪▫◊○●☺☻☼€¢£¥¤♀♂♂♠♤♣♧♥""♡♦★☆⌂№☎☏♨☜☞♩♪♫♬♭†‡←↑→↓↔↕↖↗↘↙×÷+-Ω√¼½¾⅓⅔⅛⅜⅜⅝%‰¹²³
在我的网站上结束编码:
{|}~???????????????????????????¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎ ±ÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö±øùúûüýþ?????¦???????????????¤?¢£¥¤????????""????¦???????????????????????×÷+-Ov¼½¾??????%?¹²³
解决方案:
<#ftl attributes={"content_type":"text/html"} encoding="UTF-8"/>
并将其放入我的 HttpsCoookieFilter:
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
显然我(ab)使用 HttpServlet 而不是 Freemarker 来使用 out.write() 生成 HTML 内容,所以我添加了上述内容。
这里是 servlet 源代码。任何有关如何更改它的提示都非常受欢迎:
public class HttpsCookieFilter implements Filter {
private static Logger log = Logger.getLogger(HttpsCookieFilter.class);
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse res = (HttpServletResponse) response;
req.setCharacterEncoding("UTF-8");
res.setCharacterEncoding("UTF-8");
res.setContentType("text/html; charset=UTF-8");
final HttpSession session = req.getSession(false);
if (session != null) {
setCookie(req, res);
}
try {
chain.doFilter(req, res);
} catch (IllegalStateException e) {
log.warn("HttpsCookieFilter redirect problem! ", e);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
private void setCookie(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = new Cookie("JSESSIONID", request.getSession(false).getId());
cookie.setMaxAge(-1);
cookie.setPath(getCookiePath(request));
cookie.setSecure(false);
response.addCookie(cookie);
}
private String getCookiePath(HttpServletRequest request) {
String contextPath = request.getContextPath();
return contextPath.length() > 0 ? contextPath : "/";
}
}
现在 UTF-8 无处不在 ;) 谢谢 BalusC !!!
【问题讨论】:
-
显然你并没有一直使用 UTF-8,否则一切都会好起来的。 :)
标签: java spring utf-8 character-encoding