“Invalid byte 1 of 1-byte UTF-8 sequence” error

99封情书 提交于 2020-01-17 03:27:25

问题


My error is: "Invalid byte 1 of 1-byte UTF-8 sequence".

I am calling a Java method using Blaze DS.


回答1:


Hi Nithi Make sure that "remoting-config.xml" destination id and source name are correct.




回答2:


Your XML document has a BOM marker, because it was created with a Windows program.

Java does not support this out of the box.

Regarding BOM: http://www.unicode.org/faq/utf_bom.html

So either make sure your XML Document has no BOM marker, (if it is your ds config file), or use something like this in your InputStream:

(not my code) http://koti.mbnet.fi/akini/java/unicodereader/UnicodeInputStream.java.txt

Usage pattern:
 String enc = "ISO-8859-1"; // or NULL to use systemdefault
 FileInputStream fis = new FileInputStream(file);
 UnicodeInputStream uin = new UnicodeInputStream(fis, enc);
 enc = uin.getEncoding(); // check and skip possible BOM bytes
 InputStreamReader in;
 if (enc == null) in = new InputStreamReader(uin);
 else in = new InputStreamReader(uin, enc);



回答3:


not enough details in the question.

my guess, looks like you are trying to read something as UTF-8 encoded and it is not valid UTF-8 encoded.




回答4:


ByteArrayInputStream test = new ByteArrayInputStream( xml.trim().getBytes() );
Document document = null;
try {
    document = dbf.newDocumentBuilder().parse( test );
} catch ( Exception e ) {
    System.out.println( "Fehler 1" + e.getMessage()) ;
    try {
        test.close();
        // ... that works: String xml_x = FkString.replace( xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" );
        // Replace UTF-8 to UTF8 ... works
        String xml_x = FkString.replace( xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.0\" encoding=\"UTF8\"?>" );
        test = new ByteArrayInputStream( xml_x.trim().getBytes() );
        document = dbf.newDocumentBuilder().parse( test );
    } catch ( Exception e1 ) {
        System.out.println( "Fehler 2" + e1.getMessage()) ;
    }
}


来源:https://stackoverflow.com/questions/2904638/invalid-byte-1-of-1-byte-utf-8-sequence-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!