在做接口解析时候出现的错误:
Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
很明显是在读取XML文件时候出现的编码问题!
在测试过程中发现,主要原因是xml文件中声明的编码与xml文件本身保存时的编码不一致。
现在解决的办法就有几个,主要说我测试过的两个方。
如果你是直接以文件的形式读取 可以更改XML文件中的 UTF-8编码 改为 GBK或GB2312 .
还有一种可能是 你直接以URL 通过网络地址获取InputStream流形式读取 在转换成Document对象。这种方法的解决办法是先down 下来保存在本地。实现比较简单 用个OutputStream流写到你想保存的目录即可。再解析down下来的文件 其中在 SAXReader saxReader = new SAXReader();
之后Document document=sax.read(new File(file));之前
处理XML文件编码格式即可调用下面的处理方法。
方法:
/**
* 上传文件编码判断
* */
public static String get_charset(File file) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
;
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE";
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8";
checked = true;
}
bis.reset();
if (!checked) {
// int len = 0;
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return charset;
}
/**
*down 的简单方法 保存到本地自己指定
*/
public static void writeFile(String strUrl, String filePath, String fileName) {
try {
URL url = new URL(strUrl);
InputStream is = url.openStream();
File f = new File(filePath);
f.mkdirs();
OutputStream os = new FileOutputStream(filePath + fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
/**
* 转换流编码类型方法
* */
private static byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream byteArrOut = new ByteArrayOutputStream();
byte[] temp = new byte[1024];
int len = 0;
while ((len = is.read(temp, 0, 1024)) != -1) {
byteArrOut.write(temp, 0, len);
}
byteArrOut.flush();
byte[] bytes = byteArrOut.toByteArray();
return bytes;
}
InputStream is :可以是流inputStream对象 也可以是file路径 自己转换!
在测试类里面
就可以把下面这种方式改成下面那种 (可能你不是这种方式做到):
SAXReader sax = new SAXReader();// 获得dom4j的文档对象
Document document=sax.read(new File(file));
Element element=document.getRootElement();
System.out.println(element.getName());
SAXReader saxReader = new SAXReader();
//下面转格式代码
[color=red]byte[] bytes = InputStreamToByte(new FileInputStream(file));
InputStream in = new ByteArrayInputStream(bytes);
InputStreamReader strInStream = new InputStreamReader(in,"GBK");[/color]
Document root = saxReader.read(strInStream);
Element element = root.getRootElement();
System.out.println(element.getName());
这样就可以正常输出了。
最重要的是 【转换流编码类型方法】比网上的一些解决办法来得简单多了!
分享到:
相关推荐
解决Invalid byte 1 of 1-byte UTF-8 sequence
aws-java-sdk-ec2-1.11.277.jar
赠送jar包:audience-annotations-0.5.0.jar; 赠送原API文档:audience-annotations-0.5.0-javadoc.jar; 赠送源代码:audience-annotations-0.5.0-sources.jar; 赠送Maven依赖信息文件:audience-annotations-...
解决 cvc-complex-type.2.4.d: Invalid content was found 具体参考http://henatne.iteye.com/blog/837186
esxi6.7安装net55-r8168-8.045a-napi.x86_64.VIB驱动步骤
gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-guneabi.tar.xz gcc-linaro-4.9.4-2017.01-x86_...
8)....Added: Showing call stacks for 2 new types of fatal memory errors 9)....Added: EMemLeaks._ReserveOutOfMemory to control reserve size of out of memory errors (default is 50 Mb) 10)..Added: ...
[root@monitor ~]# hpasmcli -s 'show'Invalid Arguments Zabbix监控总结起来有两种思路:第一就是server通过agentd方式获取数据,这种方式需要定义UserParameter参数,即KEY。第二就是server通过trapper的方式获取...
错误提示Invalid byte 1 of 1-byte UTF-8 sequence原因分析在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们...
离线安装包,亲测可用
invalid multibyte character sequence 870告警1
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid string or buffer length at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957) at sun.jdbc.odbc.JdbcOdbc.standardError...
赠送jar包:druid-spring-boot-starter-1.2.8.jar; 赠送原API文档:druid-spring-boot-starter-1.2.8-javadoc.jar; 赠送源代码:druid-spring-boot-starter-1.2.8-sources.jar; 赠送Maven依赖信息文件:druid-...
nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'error_view' of bean class [com.demo.controller.action.AuthorAction]: Bean property 'error_view' is not ...
赠送jar包:httpcore-4.4.13.jar; 赠送原API文档:httpcore-4.4.13-javadoc.jar; 赠送源代码:httpcore-4.4.13-sources.jar; ... ... ...人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
NULL 博文链接:https://wilian.iteye.com/blog/1992365
flink-sql-connector-mysql-cdc-2.2.1.jar flink-connector-elasticsearch7-1.15.0.jar flink-1.15.0-bin-scala_2.12.tgz
John the Ripper,是一个快速的密码破解工具,用于在已知密文...如DES、MD4、MD5等。它支持多种不同类型的系统架构,包括Unix、Linux、Windows、DOS模式、BeOS和OpenVMS,主要目的是破解不够牢固的Unix/Linux系统密码。