【问题标题】:UTF-8 decoding problems in Java & Tomcat7Java & Tomcat7 中的 UTF-8 解码问题
【发布时间】:2013-07-18 15:42:28
【问题描述】:

我正在向服务器发送 AJAX 请求,其中参数值在“escape(...)”函数中编码。

Tomcat 服务器 (7.0.42) 已配置为 s.t.接收连接器有一个 URIEncoding="UTF-8",在 web.xml 我已经配置了 SetCharacterEncodingFilter 如下:

<filter>
    <filter-name>charencode</filter-name>
    <filter-class>
        org.apache.catalina.filters.SetCharacterEncodingFilter
    </filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>charencode</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

,另外我还创建了一个过滤器来将响应编码为 UTF-8:

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
    arg1.setCharacterEncoding("UTF-8");
    arg2.doFilter(arg0, arg1);
}

解析来自拉丁字符集的参数没有问题,但是当我尝试俄语时,request.getParameter(..) 返回 null。此外,我在日志中得到了这个(怀疑它来自 SetCharacterEncodingFilter):

INFO: Character decoding failed. Parameter [usersaid] with value [%u044B%u0432%u0430%u044B%u0432%u0430%u044B%u0432%u044B%u0432%u0430%u044B%u0432%u0430%21] has been ignored. Note that the name and value quoted here may be corrupted due to the failed decoding. Use debug level logging to see the original, non-corrupted values.

并且没有 DEBUG 级别的消息可跟踪(我相信我的记录器设置正确..)

您能建议吗?很乐意回答问题!

非常感谢, 维克多。

【问题讨论】:

    标签: java character-encoding tomcat7 httprequest


    【解决方案1】:

    那个字符串没有解码。与您的应用程序服务器无关。试试这些工具,亲自看看:

    http://www.albionresearch.com/misc/urlencode.php http://meyerweb.com/eric/tools/dencoder/

    因此,错误看起来可能是客户端。确保在进行 urlencoding 时正确设置编码。您可能正在使用 UTF-8 的其他东西,这是您应该使用的。

    这里有一个关于正确编码 unicode 字符的线程:What is the proper way to URL encode Unicode characters?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 2018-06-06
    • 2018-06-17
    • 2014-10-13
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    相关资源
    最近更新 更多