17

I changed a file in Orbeon Forms, and the next time I load the page, I get an error message saying Invalid byte 2 of a 3-byte UTF-8 sequence. How can I solve this problem?

4

9 回答 9

15

This happens when Orbeon Forms reads an XML file and expects it to use the UTF-8 encoding, but somehow the file isn't properly encoded in UTF-8. To solve this, make sure that:

  1. You have an XML declaration at the beginning of the file saying the file is in UTF-8:

    <?xml version="1.0" encoding="UTF-8" ?>
    
  2. Your editor is XML-aware, so it can parse the XML declaration and consequently use the UTF-8 encoding. If your editor isn't XML aware, and you don't want to use another editor, look for an option or preference allowing you to specify that the editor must use UTF-8.

于 2012-07-03T22:29:00.237 回答
6

一个三字节的 UTF-8 序列如下所示:

1110xxxx 10xxxxxx 10xxxxxx

您的错误消息可能意味着三个字节中的第一个字节错误地标记了三个字节序列的开始,或者第二个字节格式错误。

正如@avernet 所说,您需要确保系统中的所有元素都在生成并期待 UTF-8。

于 2012-07-04T12:57:47.277 回答
6

启动程序时,请使用以下 Java 命令行参数:

-Dfile.encoding=UTF-8

例如,

java -Dfile.encoding=UTF-8 -jar foo.jar
于 2014-12-30T21:56:23.013 回答
2

我在 Eclipse 中遇到了同样的问题,我只是尝试更改文件类型。

右键单击文件 -> 资源 -> 文本文件编码(UTF-8)

这个解决方案对我有用。

谢谢。

于 2017-10-30T14:32:08.767 回答
0

我正在使用 Eclipse,我还必须更改文本文件编码:

->Windows->首选项->工作区

然后它工作得很好。

谢谢

于 2012-07-04T06:11:38.933 回答
0

您可能需要使用以下参数配置 Tomcat:

-Dfile.encoding=UTF-8

于 2013-12-16T14:29:31.363 回答
0

有同样的问题。

问题> 我正在获取 X509 证书值(多个编码源)以生成 PDF 报告。PDF 是通过等待 UTF-8 xml 请求的 web 服务生成的,我必须在编组之前重新编码这些值。

解决方案> http://fabioangelini.wordpress.com/2011/08/04/converting-java-string-fromto-utf-8/

使用这个类:

public class StringHelper {

// convert from UTF-8 -> internal Java String format
public static String convertFromUTF8(String s) {
    String out = null;
    try {
        out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
    } catch (java.io.UnsupportedEncodingException e) {
        return null;
    }
    return out;
}

// convert from internal Java String format -> UTF-8
public static String convertToUTF8(String s) {
    String out = null;
    try {
        out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
    } catch (java.io.UnsupportedEncodingException e) {
        return null;
    }
    return out;
}
}

用法:

//getSummaryAttMap() returns a HashMap
String value = (String) getSummaryAttMap().get(key);
if(value != null)
value = StringHelper.convertToUTF8(value);
else
value = "";
于 2014-04-15T11:22:10.570 回答
0

我将提供一个特殊的编码答案。当您检查 xml 文件并且没有任何问题时,您正在使用 Java 并运行 Tomcat 服务器。您的源代码可能会忽略自己指定编码,因此 JVM 在将 xml 内容作为字符串或其他表示字符串的内容读取时使用默认编码,这反过来又引用了 Tomcat 的默认编码。如果xml和Tomcat的编码不一致,也可能报同样的错误信息。

于 2017-12-13T06:24:45.993 回答
0

输入的编码切换可能会有所帮助:

XMLEventReader eventReader =
                            inputFactory.createXMLEventReader(in, 
                                    "utf-8"
                                    //"windows-1251"
                            );
于 2020-04-07T07:04:00.960 回答