2

我有一个 index.html 文件:

<!doctype html>
<html lang="fr" class="no-js fontawesome-i2svg-active fontawesome-i2svg-complete">
<head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
  <title>Annuaire Téléphonique</title>
...

我将它部署到由 Tomcat 9.0.35 服务的战争中。问题是在浏览器中,重音是这样的:“Annuaire Téléphonique”

我发现Tomcat服务的内容类型是假的:

每个 Tomcat 配置都配置为 UTF-8。我的 Linux bash 定义 LANG=UTF-8,Tomcat 以 -Dfile.encoding=UTF-8 启动

我还发现,在询问表单 index.html 时,tomcat 没有指定编码,这对我来说是完美的,因为 index.html 本身包含 UTF-8 的 Content-Type 元数据,浏览器正确显示重音

问题是:

  • 为什么 index.html 作为欢迎文件(http://127.0.0.1:10000/)被作为 ISO-8859-1 服务?
  • 它如何用作 UTF-8 ?(AddDefaultCharsetFilter UTF-8 没有帮助)

谢谢你的帮助

4

2 回答 2

0

我在公开 Spring 应用程序时找到了一个可行的解决方案,具有以下属性

spring.http.encoding.charset=UTF-8 spring.http.encoding.force-response=true

这种行为对我来说仍然很奇怪......

于 2021-10-29T06:31:38.273 回答
0

我有同样的问题:

直接访问网址:

  • example.com/index.html(编码没问题)
  • example.com/(编码错误)-> text/html;charset=ISO-8859-1

我也在使用弹簧靴。

spring.http.encoding.charset=UTF-8 对我不起作用。

这是我的解决方案:)

@Component
public class CustomFilter implements Filter
{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException
    {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        if (request instanceof HttpServletRequest)
        {
            String servletPath = ((HttpServletRequest) request).getServletPath();
            if (servletPath.equals("/")) {
                // do not let tomcat assign  text/html;charset=ISO-8859-1
                response.setContentType("text/html; charset=UTF-8");
            }
        }
        chain.doFilter(request, response);
    }
}
于 2022-01-27T15:02:14.640 回答