[英]Invalid byte 2 of 2-byte UTF-8 Java, sequence error depending on Windows/IntelliJ
So the same code is running on both of the devices, both PCs are running Windows 10, but the variable is represented different in IntelliJ debugger and one can decode string, and other can't.所以相同的代码在两台设备上运行,两台 PC 都运行 Windows 10,但变量在 IntelliJ 调试器中表示不同,一个可以解码字符串,另一个不能。 Both are accessing same endpoint.
两者都访问相同的端点。 This is code snippet that's throwing error and where code breaks.
这是抛出错误和代码中断的代码片段。 Both code are running on the same JRE and getting variable value from same endpoint.
两个代码都在同一个 JRE 上运行,并从同一个端点获取变量值。
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
As I am generating XML file that's UTF-8 encoded, it throws error for that variable.当我生成 UTF-8 编码的 XML 文件时,它会为该变量引发错误。
Variable that's not throwing error on PC1 shown in debugger未在调试器中显示的 PC1 上引发错误的变量
Variable that's throwing error on PC2在 PC2 上抛出错误的变量
I tried reinstalling all JRE, IntelliJ, etc etc, still no help...我尝试重新安装所有 JRE、IntelliJ 等,仍然没有帮助...
Are you sure the file is UTF-8 encoded?您确定该文件是 UTF-8 编码的吗? In my case I realized it was windows-1250 encoding used.
就我而言,我意识到它使用的是 windows-1250 编码。 So I had to specify an encoding:
所以我必须指定一个编码:
InputStream is = ...
InputSource src = new InputSource(new InputStreamReader(is, Charset.forName("cp1250")));
Document xmlDocument = builder.parse(src);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.