gpt4 book ai didi

java - 对从互联网获取的文本进行 UTF 8 转换

转载 作者:行者123 更新时间:2023-11-30 04:55:59 24 4
gpt4 key购买 nike

ElasticSearch 是一个搜索服务器,仅接受 UTF8 格式的数据。

当我尝试向 ElasticSearch 提供以下文本时

Small businesses potentially in line for a lighter reporting load include those with an annual turnover of less than £440,000, net assets of less than £220,000 and fewer than ten employees"

通过我的java应用程序 - 基本上我的java应用程序从网页获取此信息,并将其提供给elasticSearch。 ES 提示它无法理解 £ 并且失败了。通过以下代码过滤后 -

byte bytes[] = s.getBytes("ISO-8859-1");
s = new String(bytes, "UTF-8");

此处 £ 转换为

但是当我使用 bash 将其复制到我的主目录中的文件时,一切正常。任何指示都会有帮助。

最佳答案

您有 bytes 中的 ISO-8895-1 八位字节,然后您告诉 String 将其解码为 UTF-8。当它这样做时,它不会识别非法的 0xA3 序列并用替换字符替换它。

为此,您必须使用其使用的编码构造字符串,然后将其转换为您想要的编码。请参阅How do I convert between ISO-8859-1 and UTF-8 in Java? .

关于java - 对从互联网获取的文本进行 UTF 8 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8529705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com