`
linkyou66
  • 浏览: 228543 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

编码问题之:java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.

 
阅读更多
<p>今天在编码的时候遇到如下问题</p>
<p>java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.</p>
<p>函数源码:</p>
<p>static InputStream String2InputStream(String str) throws Exception{<br> ByteArrayInputStream stream = new ByteArrayInputStream()<br> return stream;<br>}</p>
<p>实现的功能将一个xml字符串转换为Stream类型。</p>
<p>调用时的源码</p>
<p>String ExpXmlText ="&lt;?xml version=/"1.0/" encoding=/"utf-8/"?&gt;&lt;AppSubscResult&gt;" +<br>" &lt;appId&gt;elbert_app_dsc005&lt;/appId&gt;" +<br>" &lt;longId&gt;wangwang101112347&lt;/longId&gt;" +<br>" &lt;time&gt;1236235618357&lt;/time&gt;" +<br>" &lt;sign&gt;a2e82a649b38ad10790160ec40e282af&lt;/sign&gt;" +<br>" &lt;result&gt;Yes&lt;/result&gt;" +<br>" &lt;subscUserId&gt;elb_aui_005&lt;/subscUserId&gt;" +<br>" &lt;appInstanceId&gt;USER8B38B4C13EFE7276F75889E3123C4428&lt;/appInstanceId&gt;" +<br>" &lt;gmtStart&gt;"+util.TimeHelp.getTimestamp_BeforeNDay(65)+" 00:00:00&lt;/gmtStart&gt;" +<br>" &lt;gmtEnd&gt;"+util.TimeHelp.getTimestamp_BeforeNDay(7)+" 23:59:59&lt;/gmtEnd&gt;" +<br>" &lt;subscUrl&gt;http://mall.alisoft.com/apps/subsc/subscDisplay!execute.jspa?appId=elbert_app_dsc005&lt;/subscUrl&gt;" +<br>" &lt;errorMessage&gt;" +<br>"&lt;/errorMessage&gt;" +<br>"&lt;/AppSubscResult&gt;";</p>
<p>调用</p>
<p>String2InputStream(ExpXmlText );</p>
<p>结果报:</p>
<p>java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.</p>
<p>经查时由于声明的ExpXmlText 是UTF-8编码的,而转换的时候使用默认的编码是gbk的</p>
<p>查明原因后,将原函数修订如下</p>
<p>static InputStream String2InputStream(String str) throws Exception{<br> ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes("utf-8"));<br> return stream;<br>}</p>
<p>问题得到解决</p>
<p></p>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics