首页 > 代码库 > xml:Invalid byte 2 of 2-byte UTF-8 sequence

xml:Invalid byte 2 of 2-byte UTF-8 sequence

xml解析报错:Invalid byte 2 of 2-byte UTF-8 sequence

在做接口解析时候出现的错误: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();

正常写法:

private ImpowerDeviceVo getResponseXml(String requestUrl, String requestXml) throws Exception
{
  if (StringUtils.isEmpty(requestUrl))
  {
   String message = "config-WebPortal.xml配置文件未配置,获取接口连接为空!";
   logger.debug(message);
   logger.error(message);
  }
 
  URL httpurl = new URL(requestUrl);
  HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection();
  httpConn.setConnectTimeout(ImpowerXmlNodeName.connectTimeout);
  httpConn.setReadTimeout(ImpowerXmlNodeName.readTimeout);
  httpConn.setRequestMethod(ImpowerXmlNodeName.requestMethod);
  httpConn.setDoInput(true);
  // 将doOutput标志设置为true,指示应用程序要将数据写入URL连接。
  httpConn.setDoOutput(true);
  OutputStream out = httpConn.getOutputStream();
  logger.debug("requestXml:" + requestXml);
  // 将参数写入URL接口连接
  out.write(requestXml.getBytes("utf-8"));
 
  SAXBuilder sax = new SAXBuilder();
  Document doc = sax.build(httpConn.getInputStream());
 
  // 获取到信息
  ImpowerDeviceVo impowerDeviceVo = this.getImpowerDeviceVoByDocument(doc);
 
  httpConn.disconnect();
  out.close();

  return impowerDeviceVo;
}


修改方法如下:

造一个临时文件保存所定的编码报文,然后读取,读取解析后关闭流,再删除文件,否则会报文件读取多误 read error

private ImpowerDeviceVo getResponseXml(String requestUrl, String requestXml) throws Exception
{
  if (StringUtils.isEmpty(requestUrl))
  {
   String message = "config-WebPortal.xml配置文件未配置,获取接口连接为空!";
   logger.debug(message);
   logger.error(message);
  }
 
  URL httpurl = new URL(requestUrl);
  HttpURLConnection httpConn = (HttpURLConnection) httpurl.openConnection();
  httpConn.setConnectTimeout(ImpowerXmlNodeName.connectTimeout);
  httpConn.setReadTimeout(ImpowerXmlNodeName.readTimeout);
  httpConn.setRequestMethod(ImpowerXmlNodeName.requestMethod);
  httpConn.setDoInput(true);
  // 将doOutput标志设置为true,指示应用程序要将数据写入URL连接。
  httpConn.setDoOutput(true);
  OutputStream out = httpConn.getOutputStream();
  logger.debug("requestXml:" + requestXml);
  // 将参数写入URL接口连接
  out.write(requestXml.getBytes("utf-8"));
 

  InputStream inputStream = httpConn.getInputStream();
  String fileName = "tempxml.xml";
 
  // 保存文件
  File f = new File(fileName);
  if (!f.exists())
  {
   f.createNewFile();
  }
  String xmlContent = readXmlStream(inputStream);
  System.out.println("进入-----------"+xmlContent);
  FileOutputStream fileOutputStream = new FileOutputStream(f);
  fileOutputStream.write(xmlContent.getBytes("utf-8"));
  fileOutputStream.close();
  // 打开文件
  f = new File(fileName);
  FileInputStream fileInputStream = new FileInputStream(f);
 
  SAXBuilder sax = new SAXBuilder();
//  Document doc = sax.build(httpConn.getInputStream());
  Document doc = sax.build(fileInputStream);
 
  // 获取到信息
  ImpowerDeviceVo impowerDeviceVo = this.getImpowerDeviceVoByDocument(doc);
 
  httpConn.disconnect();
  out.close();
  fileInputStream.close();
  f.delete();

  return impowerDeviceVo;
}