判断网页的编码我们主要是为了在解析网页时出现一些乱码问题。主要用到了两个jar包
chardet.jar和cpdetector_1.0.7.jar。
具体的使用如下:
主要是读取页面的头文件的一段字符,然后分析。当然也可以变换下测试文本或其他类型文件的编码。
chardet.jar和cpdetector_1.0.7.jar。
具体的使用如下:
public class BianMa {
public static void main(String[] args){
String path="";
CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(true));
detector.add(JChardetFacade.getInstance());
//ASCIIDetector用于ASCII编码测定
detector.add(ASCIIDetector.getInstance());
//UnicodeDetector用于Unicode家族编码的测定
detector.add(UnicodeDetector.getInstance());
java.nio.charset.Charset charset = null;
File f=new File("C:\\TEST\\13128931.html");
try {
charset = detector.detectCodepage(new BufferedInputStream(new FileInputStream(f)),100);
} catch (Exception ex) {ex.printStackTrace();}
if(charset!=null){
System.out.println(f.getName()+"编码是:"+charset.name());
}else{
System.out.println(f.getName()+"未知");
}
}
}
主要是读取页面的头文件的一段字符,然后分析。当然也可以变换下测试文本或其他类型文件的编码。