【问题标题】:Invalid byte 2 of a 3-byte UTF-8 sequence when i execute the Build project执行构建项目时 3 字节 UTF-8 序列的字节 2 无效
【发布时间】:2015-07-15 22:07:06
【问题描述】:

项目链接:https://github.com/GunB/eExcelParser 我正在尝试更改基于 metadata.xml 的 XML,当我运行 Netbeans 进行编译时它可以正常工作,但是当我创建 Clean and Build 版本时它会抛出 Invalid byte 2 of a 3-byte UTF-8 sequence ...我不明白为什么

我正在使用此代码来读取 XML 文件

File fXmlFile = new File(strBase);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(fXmlFile);

我正在使用的库是POIGuava、POI 中包含的 XMLBeans 和 dom4j

项目也发送此警告:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Warning: Setting Codebase manifest attribute to '*' due to current JNLP Codebase. Set manifest.custom.codebase property to override the non-secure value '*'.

【问题讨论】:

    标签: java xml utf-8


    【解决方案1】:

    XML 文件似乎被转换为错误的单字节编码。检查使用Reader/Writer/String.getBytes/new String 是否始终指定一个字符集。不要使用FileReader/FileWriter

    【讨论】:

    • 但为什么 netbeans debug 会正确读取 XML? (我还没有使用你的答案,我正在使用它)
    • 您可能正在使用 maven 或 ant 应用错误的重新编码。到处指定 UTF-8:IDE 也是。然后有时会有一个复制过滤资源文件,它允许表达式语言替换,也可能破坏 XML 文件,在结果中复制和重新编码。检查 jar 中的 XML。
    • 但那是奇怪的部分,XML 不在罐子里。它类似于同一文件夹中的“program.jar”和“metadata.xml”。文件读取正确但解析错误
    • 并且<?xml...> 编码是正确的。使用像 JEdit 或 NotePad++ 这样可以进行编码的程序员编辑器。进行十六进制转储(非 7 位 ASCII)。如果没问题,则在某个地方涉及到 Reader 或 Writer。不要放弃。
    • 使用你的答案我得到了我想要的,现在我遇到了另一个问题,但是,一步一步:)
    【解决方案2】:

    我尝试了以下方法,效果很好:

    public static void main(String[] args) throws Exception{
        DocumentBuilderFactory Factory;
        Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = Factory.newDocumentBuilder();
        Document doc = parser.parse("https://raw.githubusercontent.com/GunB/eExcelParser/develop/metadata.xml");
    }
    

    可能是你没有正确保存文件; 我还使用 curl 保存了metada.xml

    curl -O https://raw.githubusercontent.com/GunB/eExcelParser/develop/metadata.xml
    

    当我解析本地文件时,它也可以正常工作

    【讨论】:

    • 当我使用干净和构建版本时它继续发送相同的错误......我认为它在构建项目或其他东西时与配置有关...... idk。我也尝试使用你在这里建议的 curl 下载文件
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 2011-01-26
    • 2011-12-25
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 2012-07-04
    • 2015-06-15
    相关资源
    最近更新 更多