使用jchardet1.1 判斷檔案或網頁編碼

來源:互聯網
上載者:User

標籤:jchardet   java字元編碼   字元編碼   utf-8   gb2312   

package org.shefron.utils;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.net.URL;import java.util.Arrays;import org.mozilla.intl.chardet.nsDetector;import org.mozilla.intl.chardet.nsICharsetDetectionObserver;import org.mozilla.intl.chardet.nsPSMDetector;public final class FileCharsetDetector {/** * * @param filePath the local filepath * @param langFlag *            nsPSMDetector.CHINESE, *            nsPSMDetector.SIMPLIFIED_CHINESE, *            nsPSMDetector.TRADITIONAL_CHINESE, *            nsPSMDetector.ALL ... * @return * @throws Exception */public final static CharsetObj getPriorityCharset(String filePath, int langFlag) throws Exception {if(langFlag <1 || langFlag>6){langFlag = nsPSMDetector.ALL;}final CharsetObj charsetObj = new CharsetObj();FileInputStream fis = null;try {fis = new FileInputStream(filePath);nsDetector det = new nsDetector(langFlag);// Set an observer...// The Notify() will be called when a matching charset is found.det.Init(new nsICharsetDetectionObserver() {public void Notify(String charset) {charsetObj.setFound(true);charsetObj.setCharset(charset);}});BufferedInputStream imp = new BufferedInputStream(fis);byte[] buf = new byte[1024];int len;boolean done = false;boolean isAscii = true;while ((len = imp.read(buf, 0, buf.length)) != -1) {// Check if the stream is only ascii.if (isAscii)isAscii = det.isAscii(buf, len);// DoIt if non-ascii and not done yet.if (!isAscii && !done)done = det.DoIt(buf, len, false);}det.DataEnd();if (isAscii) {charsetObj.setAscii(true);}charsetObj.setProbableCharsets(det.getProbableCharsets());} catch (Exception e) {System.out.println("File handler Error!");throw new Exception("File handler Error:"+e.getMessage());}return charsetObj;}/** * * @param filePath the local filepath * @return * @throws Exception */public final static CharsetObj getPriorityCharset(String filePath) throws Exception{return getPriorityCharset(filePath, nsPSMDetector.ALL);}/** * * @param url the www url * @return * @throws Exception */public final static CharsetObj getPriorityCharset(URL url) throws Exception{return getPriorityCharset(url, nsPSMDetector.ALL);}/** * * @param url the www url * @param langFlag * @return * @throws Exception */public static CharsetObj getPriorityCharset(URL url, int langFlag) throws Exception {if(langFlag <1 || langFlag>6){langFlag = nsPSMDetector.ALL;}final CharsetObj charsetObj = new CharsetObj();try {nsDetector det = new nsDetector(langFlag);// Set an observer...// The Notify() will be called when a matching charset is found.det.Init(new nsICharsetDetectionObserver() {public void Notify(String charset) {charsetObj.setFound(true);charsetObj.setCharset(charset);}});BufferedInputStream imp = new BufferedInputStream(url.openStream());byte[] buf = new byte[1024];int len;boolean done = false;boolean isAscii = true;while ((len = imp.read(buf, 0, buf.length)) != -1) {// Check if the stream is only ascii.if (isAscii)isAscii = det.isAscii(buf, len);// DoIt if non-ascii and not done yet.if (!isAscii && !done)done = det.DoIt(buf, len, false);}det.DataEnd();if (isAscii) {charsetObj.setAscii(true);}charsetObj.setProbableCharsets(det.getProbableCharsets());} catch (Exception e) {System.out.println("File handler Error!");throw new Exception("File handler Error:"+e.getMessage());}return charsetObj;}public final static class CharsetObj {private boolean found = false;private String[] probableCharsets = null;private String charset = null;public String getCharset() {return charset;}@Overridepublic String toString() {return "CharsetObj [charset=" + this.getCharset() + ", found=" + this.isFound()+ ", isAscii=" + this.isAscii() + ", probableCharsets="+ Arrays.toString(this.getProbableCharsets()) + "]";}public void setCharset(String charset) {this.charset = charset;}private boolean isAscii = true;public boolean isFound() {return found;}public String[] getProbableCharsets() {return probableCharsets;}public boolean isAscii() {return isAscii;}public void setFound(boolean found) {this.found = found;}public void setProbableCharsets(String[] probableCharsets) {this.probableCharsets = probableCharsets;}public void setAscii(boolean isAscii) {this.isAscii = isAscii;}}}

使用jchardet1.1 判斷檔案或網頁編碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.