【问题标题】:Jetty response character encoding码头响应字符编码
【发布时间】:2015-10-26 14:15:13
【问题描述】:

如何在对 UTF-8 的响应中设置默认字符编码?

我试过了

    System.setProperty("file.encoding", "UTF-8");

还有这个

    System.setProperty("org.eclipse.jetty.util.UrlEncoding.charset", "utf-8");

两者都没有任何效果 - 响应仍然与标头一起发送

Content-Type: text/html; charset=ISO-8859-1

我想对所有 text/html 响应执行此操作,最好是使用代码而不是 XML。我正在使用 Jetty 9。

【问题讨论】:

    标签: java encoding character-encoding jetty content-type


    【解决方案1】:

    Jetty 文档声称它默认使用 UTF-8,但这似乎是一个谎言。如果做正常的response.getWrite().println("Hello"),那么内容编码确定如下。

    1. 从 org/eclipse/jetty/http/encoding.properties 加载从内容类型到内容编码的默认映射:
            // MimeTypes.java:155
            ResourceBundle encoding = ResourceBundle.getBundle("org/eclipse/jetty/http/encoding");
            Enumeration<String> i = encoding.getKeys();
            while(i.hasMoreElements())
            {
                String type = i.nextElement();
                __encodings.put(type,encoding.getString(type));
            }
    

    默认文件为:

    text/html   = ISO-8859-1
    text/plain  = ISO-8859-1
    text/xml    = UTF-8
    text/json   = UTF-8
    
    1. Response.getWriter() 尝试使用该映射,但默认为 ISO-8859-1
    @Override
    public PrintWriter getWriter() throws IOException
    {
        if (_outputType == OutputType.STREAM)
            throw new IllegalStateException("STREAM");
    
        if (_outputType == OutputType.NONE)
        {
            /* get encoding from Content-Type header */
            String encoding = _characterEncoding;
            if (encoding == null)
            {
                encoding = MimeTypes.inferCharsetFromContentType(_contentType);
                if (encoding == null)
                    encoding = StringUtil.__ISO_8859_1;
                setCharacterEncoding(encoding);
            }
    

    所以你可以看到text/html 不默认为 UTF-8。我认为没有办法从代码中更改默认值。最好的办法是将encoding.properties 文件更改为:

    text/html   = UTF-8
    text/plain  = UTF-8
    text/xml    = UTF-8
    text/json   = UTF-8
    

    但即便如此,即使它找到了不存在的编码,它也会默认为 ISO-8859-1。

    【讨论】:

      【解决方案2】:
      response.setCharacterEncoding("UTF-8");
      

      【讨论】:

      • 默认字符编码
      【解决方案3】:

      什么时候使用 Writer();

      如果我写的话对我来说

      resp.getWriter().println("Return");
      resp.setContentType("text/html; charset=UTF-8");
      

      我不会工作

      但是如果我改变顺序

      resp.setContentType("text/html; charset=UTF-8");
      resp.getWriter().println("Return");
      

      会好的

      【讨论】:

        【解决方案4】:

        我为一个旧版应用程序创建了字符编码过滤器。

        public class CharacterEncodingFilter implements Filter {
        
            @Override
            public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                if(req instanceof Request){             
                    req.setCharacterEncoding("UTF-8");
                }
                chain.doFilter(req, res);
            }
        
            @Override
            public void init(FilterConfig arg0) throws ServletException {
            }
        
            @Override
            public void destroy() {
            }
        }
        

        在 web.xml 中,filter-mapping 的 url-pattern 为 /*。这会通过 CharacterEncodingFilter 路由来自 Web 应用程序的所有请求。

        <filter>
            <display-name>CharacterEncoding</display-name>
            <filter-name>CharacterEncoding</filter-name>
            <filter-class>my.app.filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        

        【讨论】:

        • 为什么将reqests字符编码设置为utf-8?我认为它应该在响应对象上。
        【解决方案5】:

        例如,您可以将默认的 UTF-8 字符集更改为 ISO-8859-1。 文档没有很清楚 9.3 以上版本的参数名称。 在 9.3 之前是org.eclipse.jetty.util.URI.charset 对于新版本,它已更改为org.eclipse.jetty.util.UrlEncoding.charset 这是一个例子:

        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.15.v20190215</version>
            <configuration>
                <systemPropertiesFile>src/main/config/jetty/encode.properties</systemPropertiesFile>
                <jettyXml>src/main/config/jetty/jetty-env.xml</jettyXml>
            </configuration>    
        </plugin>
        

        encode.properties 的内容

        org.eclipse.jetty.util.UrlEncoding.charset=ISO-8859-1
        

        【讨论】:

        • 这是用于 URI / URL 编码的。我们正在谈论>>内容
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多