1. 程式人生 > >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;
}

相關推薦

xmlInvalid 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檔案時候出現的編碼問題! 在測試過程中發現,主要原因是xm

錯誤“Invalid byte 2 of 2-byte UTF-8 sequence

曾幾次在啟動tomcat,載入專案配置檔案Deploying configuration descriptor ***.xml時報錯Invalid byte 2 of 2-byte UTF-8 sequence 發現一般是xml宣告(或預設)編碼與檔案中文字的編碼不一致造成

xml中1位元組的UTF-8序列的位元組1無效([字元編碼]Invalid byte 1 of 1-byte UTF-8 sequence終極解決方案)

  xml中1位元組的UTF-8序列的位元組1無效([字元編碼]Invalid byte 1 of 1-byte UTF-8 sequence終極解決方案) 專案本地執行是ok的,但是釋出到線上伺服器就一直報錯,說什麼   Error

異常 Invalid byte 3 of 3-byte UTF-8 sequence 處理解決

異常: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from file [/usr/local/AppStoreServe

錯誤解決 :Invalid byte 3 of 3-byte UTF-8 sequence.

最近使用ant 多渠道打包  老是編譯不成功 提示如下錯誤  I/O error for /wemicommunity/AndroidManifest.xml: Invalid byte 3 of

位(bit),位元組(Byte),KB,MB,GB,TB,UTF-8,Unicode,字符集,排序規則

1位元組(byte) = 8位(bit) 1KB=1024byte 1MB=1024KB 1GB=1024MB 位(bit):位只有兩種形式0和1 位元組(byte):位元組是有8個位組成的。可以表示256個狀態。1位元組(byte)=8位(bit) 一個utf8數字佔1個

js byte[] 和string 相互轉換 UTF-8

function stringToByte(str) { var bytes = new Array(); var len, c; len = str.length; for(var i = 0; i < len; i++) { c = str.charCode

用java實現簡單快速的webservice客戶端/資料採集器(支援soap1.1和soap1.2標準,支援utf-8編碼)

前言: 用了cxf,axis等各種wbeservice實現庫,簡單試用了一下動態呼叫的方式,很不滿意,完全無法滿足業務的需要,所以自己實現了一個webservice採集客戶端,方便動態呼叫外部webservice介面。 一、實現的功能 1、soap1.1客戶端(soap1.

Mac電腦使用解決Mac上“文字編碼Unicode(UTF-8)不適用”、文字編碼“中文 (GB 18030)不適用“的問題

在Mac電腦上面開啟txt檔案,有些時候由於格式不一樣或者其他原因,會打不開txt檔案,這樣就需要我們對文字編輯的偏好設定裡面進行修改即可。這裡我只說兩種打不開的情況,這兩種情況就是標題說的這兩種情況。 一、先介紹第一種情況:未能開啟文稿“docs(1).txt”。文字編碼“Unicode(

王權富貴出錯集Python指令碼開頭兩行的#!/usr/bin/env python和# -*- coding: utf-8 -*-的作用

1.#!/usr/bin/env python 是用來說明指令碼語言是 python 的 是要用 /usr/bin下面的程式(工具)python,這個直譯器,來解釋 python 指令碼,來執行 python 指令碼的。     2、# -*- coding:

c#字元編碼,System.Text.Encoding類,字元編碼大全如Unicode編碼、GB18030、UTF-8UTF-7,GB2312,ASCII,UTF32,Big5

本頁列出來目前window下所有支援的字元編碼  ---c#通過 System.Text.Encoding.GetEncodings()獲取,裡面可以對其進行查詢,篩選,對同一個字元,在不同編碼進行檢視和分析。。。 內碼表編號 名稱 程式碼 型別 單位元組碼位 37 IB

tomcat部署新的項目啟動時出現報錯信息 Invalid byte tag in constant pool: 15

tomcat 啟動報錯 invalid byte tag in constant pool ......org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 15

【Python】讀取cvs文件報錯UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 6: invalid start byte

spl 重慶 http posit nbsp div ack lin pan 現在有文件data.csv 文件編碼格式為:ANSI data.csv 1|1|1|北京市 2|1|2|天津市 3|1|3|上海市 4|1|4|重慶市 5|1|5|石家莊市 6|

python 讀取資料出現UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid contin

之前寫程式時也出現過類似錯誤,每次解決了到第二次遇見又忘了具體方法,這次記錄一下。 一、字元編碼問題 先介紹一下字元編碼問題 1.ASCLL與GB2312 由於計算機是美國人發明的,因此,最早只有127個字元被編碼到計算機裡,也就是大小寫英文字母、數字和一些符號,這個編碼表被稱為

eclipse web module專案倒匯入版本問題Cannot change version of project facet Dynamic Web Module to 2.5.

專案匯入時出現Cannot change version of project facet Dynamic Web Module to 2.5.問題: 問題描述: 解決方法: 開啟工程目錄下的.settings資料夾,修改org.eclipse.wst.common.project.fac

python 讀取資料出現UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: invalid contin

之前寫程式時也出現過類似錯誤,每次解決了到第二次遇見又忘了具體方法,這次記錄一下。 一、字元編碼問題 先介紹一下字元編碼問題 1.ASCLL與GB2312 由於計算機是美國人發明的,因此,最早只有127個字元被編碼到計算機裡,也就是大小寫英文字母、數字和一些符號,這個

錯誤Component is part of the declaration of 2 modules

..... import { AddEventModule } from './add-event.module'; // <-- don't forget to import the AddEventModule class @NgModule({ decl

2.6.2 XML配置使用testNG進行並發多瀏覽器測試

firefox pla sig ret 分享圖片 使用 system exce ignorecas 測試類 1 @Parameters("browser") 定義browser參數。 在測試執行過程中,browser參數具體值由XML

Oracle數據庫12cR2(項目實戰之二)Linux系統安裝Oracle12.2

oracle12cr2 oracle視頻教程 oracle12cr2安裝 linux上安裝12c linux上安裝12cr2 oracle數據庫12cR2(項目實戰之二):linux系統安裝Oracle12.2一、課程主題:風哥Oracle數據庫教程12cR2(項目實戰之二):在linux

2.7.2 元素定位frame 內定位 driver.switch_to.frame()

path 第一個 process switch -a element ren nbsp 定位 來源: http://blog.csdn.net/anniejunyan/article/details/23257327 Selenium + Webdriver 學習(五)