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;
}

posted @ 2016-04-13 17:32  phlsheji  阅读(2645)  评论(0编辑  收藏  举报