长期运行的爬虫程序(抓取xml)突然出了问题。xml的乱码导致无法验证通过
发现不同的网站返回的xml编码不一致,有的是gb2312,有的utf-8。
爬虫程序将urlConnection.getInputStream() 的字节流传递给了SAXReader来构造Document
可惜SAXReader还不够强悍,由于只是获取了字节流,但不知道编码方式,于是SAXReader采用了系统默认的编码方式对对待字节流,问题就出在这里。
if(encoding == null)
reader = new InputStreamReader(stream);
sd = StreamDecoder.forInputStreamReader(in, this, (String)null)
编码方式为空
(InputStream in,Object lock,String charsetName)
if (csn == null)
csn = Charset.defaultCharset().name();
获取默认编码方式
java.security.PrivilegedAction pa =
new GetPropertyAction("file.encoding");
String csn = (String)AccessController.doPrivileged(pa);
Charset cs = lookup(csn);
if (cs != null)
defaultCharset = cs;
else
defaultCharset = forName("UTF-8");
首先参考-Dfileencoding,如果没有就是系统默认字符编码,还找不到就是“UTF-8”
如果 在eclipse中运行程序,eclipse会指定-Dfileencoding, 值就是你得文件编码
参考com.sun.syndication.io.XmlReader
取头3个字节
UTF_16BE:0xFE 0xFF
UTF_16LE:0xFF 0xFE
UTF_8: 0xEF 0xBB 0xBF
实际通过测试发现:
//utf-16BE、utf-16LE、utf-16,utf-8编码差别
System.out.println(Arrays.toString("<".getBytes("utf-16BE"))); :[0, 60]
System.out.println(Arrays.toString("<".getBytes("utf-16LE"))); :[60, 0]
System.out.println(Arrays.toString("<".getBytes("utf-16"))); :[-2, -1, 0, 60]
System.out.println(Arrays.toString("<".getBytes("utf-8"))); :[60]
//能识别BOM?
byte[] b1=new byte[]{-2,-1,0,60};
System.out.println(new String(b1,"UTF-16BE")); // <
System.out.println(new String(b1,"UTF-16")); // <
byte[] b1=new byte[]{-1,-2,60,0};
System.out.println(new String(b1,"UTF-16LE")); // ?<
System.out.println(new String(b1,"UTF-16")); // <
byte[] b1=new byte[]{-17,-69,-65,60};
System.out.println(new String(b1,"UTF-8")); // ?<
上面红色代表错误 ,绿色代表正确
可见java中的BOM纯粹是为UTF-16 big endian 或者little endian准备,基本上已不具备识别UTF-16BE、UTF-16LE、UTF-16、UTF-8功能
取头4个字节,看是否匹配<?xm
UTF_16BE: 0x00 0x3C 0x00 0x3F
UTF_16BE: 0x3C 0x00 0x3F 0x00
UTF_8: 0x3C 0x3F 0x78 0x6D
采用第一种
经过上面的步骤输入正确了,Document也成功解析了,为什么 org.dom4j.Document.asXML() 仍然乱码?
看看代码:
public String asXML() {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLWriter writer = new XMLWriter(out, outputFormat);
writer.write(this);
return out.toString();
}
catch (IOException e) {
throw new RuntimeException("IOException while generating textual representation: " + e.getMessage());
}
}
out.toString()
return new String(buf, 0, count);
char[] v = StringCoding.decode(bytes, offset, length);
String csn = Charset.defaultCharset().name();
try {
return decode(csn, ba, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
采用了系统默认编码来输出导致问题
需要看看XMLWriter采用了何种编码写入document
this.writer = createWriter(out, format.getEncoding());
采用了format.getEncoding()
知道了写入时的编码就好说了
out.toString(outputFormat.getEncoding());
当程序处理字节流的时候,必须想办法知道字节的编码方式,否者就会出问题