判断文件编码

公司有个简历上传的需求,由于文件编码不同,需要自动判断文件编码;

//判断文件编码
		CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
		detector.add(JChardetFacade.getInstance());
		detector.add(ASCIIDetector.getInstance());
		detector.add(UnicodeDetector.getInstance());
		Charset charset = null;
		try {
			charset = detector.detectCodepage(new BufferedInputStream(new FileInputStream(resumeFile)), 100);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (charset != null) {
			return charset.name();
		}
		return null;

根据获取的编码进行读取:

InputStreamReader read = new InputStreamReader(new FileInputStream(resumeFile), filecode);

用到的开源包: cpdetector

你可能感兴趣的:(编码,乱码,Cpdetector)