【发布时间】:2020-10-07 13:11:01
【问题描述】:
我有一个带有 BOM(UTF-8 编码)的 xml 文件。该文件以byte[] 的形式出现。我需要跳过 BOM,然后将这些字节转换为字符串。
这就是我的代码现在的样子:
BOMInputStream bomInputStream = new BOMInputStream(new ByteArrayInputStream(requestDTO.getFile())); // getFile() returns byte[]
bomInputStream.skip(bomInputStream.hasBOM() ? bomInputStream.getBOM().length() : 0);
validationService.validate(new String(/*BYTE[] WITHOUT BOM*/)); // throws NullPointerException
我正在使用 BOMInputStream。我有几个问题。第一个是bomInputStream.hasBOM() 返回false。第二个,我不知道以后如何从bomInputStream 中检索byte[],因为bomInputStream.getBOM().getBytes() 会抛出NullPointerException。感谢您的帮助!
BOMInputStream 文档链接: https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/input/BOMInputStream.html
【问题讨论】:
标签: java spring byte-order-mark