1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > org.xml.sax.SAXParseException: Content is not allowed in trailing section

org.xml.sax.SAXParseException: Content is not allowed in trailing section

时间:2020-11-26 20:21:27

相关推荐

org.xml.sax.SAXParseException: Content is not allowed in trailing section

独角兽企业重金招聘Python工程师标准>>>

org.xml.sax.SAXParseException: Content is not allowed in trailing section 博客分类: 异常

总结一下,这个问题主要出现的原因有两点。

1、XML内容不正确,比如多个空格,换行等等。需要仔细排查;

2、使用Stream读取流文件不正确,详细如下:

读写文件时,一般采用的是每次从inputStream中read 一个有固定大小的byte array时,这时候在

byte[] array = new byte[1024];

BufferedInputStream buffInput = new BufferedInputStream(inputStream);

FileOutputStream fileOS = new FileOutputStream(targetFile);

BufferedOutputStream buffOS = new BufferedOutputStream(fileOS);

int word = 0;

while ((word = buffInput.read(array)) != -1){

buffOS.write(array);

}

上面的写法,如果最后一次read到array中不足1024的时候,前一次read的byte同样会出现在byte[]中,从而导致XML解析失败。因此可以采用下面的方法:

BufferedInputStream buffInput = new BufferedInputStream(inputStream);

FileOutputStream fileOS = new FileOutputStream(targetFile);

BufferedOutputStream buffOS = new BufferedOutputStream(fileOS);

int word = 0;

while ((word = buffInput.read()) != -1){

buffOS.write(word);

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。