【问题标题】:Http response decoding behaves differently from Windows to Linux从 Windows 到 Linux,Http 响应解码的行为不同
【发布时间】:2021-06-07 08:16:06
【问题描述】:

我的 java 应用程序正在从 bitbucket 存储库下载一些使用 UTF-8ISO-8859-1 编码的文件。
我事先知道这些文件中使用的字符集。

我的应用程序在我的 Windows 本地机器上运行良好(我使用 Eclipse JEE 和 Tomcat 9 服务器)。

我已将此应用程序部署在运行相同版本 Tomcat 的 RedHat 虚拟机上,并且 我最终用未知字符 替换了这些 é/è/à/ù/ï


这是我为获取此数据而编写的代码:
public static String getFileContentFromRepository(String url) throws IOException {
        HttpURLConnection connection = getConnection(url);
        connection.connect();

        //The following function returns the charset of the file. (Proven to work)
        Charset repoCharset = getCharset();

        InputStream connectionDataStream = connection.getInputStream();
        String connectionStreamData = IOUtils.toString(connectionDataStream, repoCharset);
        
        connection.disconnect();

        return connectionStreamData;
}

如何在两个平台上获得相同的结果?

【问题讨论】:

    标签: java linux windows http character-encoding


    【解决方案1】:

    问题来自 Linux 的默认字符集设置为 UTF-8。
    在 Tomcat 的配置中将参数 -Dfile.encoding=ISO-8859-1 添加到 $CATALINA_OPTS 解决了我的问题。

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多