今天在编码的时候遇到如下问题
java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
函数源码:
 static InputStream String2InputStream(String str) throws Exception{
     ByteArrayInputStream stream = new ByteArrayInputStream()
     return stream;
 }
实现的功能将一个xml字符串转换为Stream类型。
调用时的源码
  String ExpXmlText ="" +
    "  elbert_app_dsc005" +
    "  wangwang101112347" +
    "  " +
    "  a2e82a649b38ad10790160ec40e282af" +
    "  Yes" +
    "  elb_aui_005" +
    "  USER8B38B4C13EFE7276F75889E3123C4428" +
    "  "+util.TimeHelp.getTimestamp_BeforeNDay(65)+" 00:00:00" +
    "  "+util.TimeHelp.getTimestamp_BeforeNDay(7)+" 23:59:59" +
    "  http://mall.alisoft.com/apps/subsc/subscDisplay!execute.jspa?appId=elbert_app_dsc005" +
    "  " +
    "
" +
    "
";
调用
String2InputStream(ExpXmlText );
结果报:
java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence.
经查时由于声明的ExpXmlText 是UTF-8编码的,而转换的时候使用默认的编码是gbk的
查明原因后,将原函数修订如下
 static InputStream String2InputStream(String str) throws Exception{
     ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes("utf-8"));
     return stream;
 }
问题解决