遇到这类错误,一般都是编码问题导致,文件编码、保存时的编码都要保持一致,如果尝试了好多次并且确定所有编码均为UTF-8还报下面的错误,
建议将xml文件顶部的encoding="UTF-8" 改为 encoding="GBK"
详细错误:
严重: Servlet.service() for servlet ReportServlet threw exception
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:674)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:425)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1742)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.arrangeCapacity(XMLEntityScanner.java:1619)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(XMLEntityScanner.java:1657)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:193)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
大概就这样。
这样的问题一般都是编码不一致所致。
到这里如果还是有问题,那么再推荐一种做法,我是用的这种方式解决的,就是改变读取方式,在我这里造成前面所有错误的根源是我使用了Reader读取配置文件,那么这里我将Reader换了,原来报错的代码片段:
1 package com.hedh.Util; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 10 public class MybatisUtils { 11 private final static SqlSessionFactory SQL_SESSION_FACTORY; 12 static{ 13 String resource="mybatis-config.xml"; 14 Reader reader=null; 15 try{ 16 reader = Resources.getResourceAsReader(resource); 17 }catch(IOException e){ 18 e.printStackTrace(); 19 } 20 SQL_SESSION_FACTORY=new SqlSessionFactoryBuilder().build(reader); 21 } 22 public static SqlSessionFactory getSqlSessionFactory(){ 23 return SQL_SESSION_FACTORY; 24 } 25 }
将代码中的Reader更换了(注意两段代码的14行、16行、20行):
1 package com.hedh.Util; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSessionFactory; 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 9 10 public class MybatisUtils { 11 private final static SqlSessionFactory SQL_SESSION_FACTORY; 12 static{ 13 String resource="mybatis-config.xml"; 14 InputStream in = null; 15 try{ 16 in = Resources.class.getResourceAsStream(resource); 17 }catch(IOException e){ 18 e.printStackTrace(); 19 } 20 SQL_SESSION_FACTORY=new SqlSessionFactoryBuilder().build(in); 21 } 22 public static SqlSessionFactory getSqlSessionFactory(){ 23 return SQL_SESSION_FACTORY; 24 } 25 }
这样子我的错误问题是解决了。问题根源我也想的还不是太清楚,好像是读取方式的问题。