123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package com.toolkit.file.encode;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import info.monitorenter.cpdetector.io.*;
- public class GetfilesEncode{
- /**
- * 需要检查的文件类型
- */
- static String[] ScanExtName = { "jsp", "html", "js", "xml", "java",
- "json", "css" };
- private String toEncode = "";
- private ArrayList<Map<String, String>> fileList = new ArrayList<Map<String, String>>();
- public GetfilesEncode(String fileForder, String toEncode) {
- this.toEncode = toEncode;
- File objFile = new File(fileForder);
- try {
- doDealWithObj(objFile);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void doDealWithObj(File objFile) throws Exception {
- if (objFile.isDirectory()) {
- File[] objFiles = objFile.listFiles();
- for (int i = 0; i < objFiles.length; i++) {
- if (objFiles[i].isDirectory()) {
- doDealWithObj(objFiles[i]);
- } else {
- // getFileEncode(objFiles[i]);
- if (isScan(getFileExt(objFiles[i]))) {
- getFileEncodeByDetector(objFiles[i]);
- }
- }
- }
- } else {
- if (isScan(getFileExt(objFile))) {
- getFileEncodeByDetector(objFile);
- }
- }
- }
- /**
- * 获得文件的编码格式类型
- *
- * @param afile
- */
- @SuppressWarnings("deprecation")
- private void getFileEncodeByDetector(java.io.File afile) {
- CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); // A
- // singleton.
- /*-------------------------------------------------------------------------
- ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
- 指示是否显示探测过程的详细信息,为false不显示。
- ---------------------------------------------------------------------------*/
- detector.add(new ParsingDetector(false));
- detector.add(new ParsingDetector(true)); // be verbose about parsing.
- detector.add(new ByteOrderMarkDetector());
- /*--------------------------------------------------------------------------
- JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
- 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
- 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
- ---------------------------------------------------------------------------*/
- detector.add(JChardetFacade.getInstance());
- // ASCIIDetector用于ASCII编码测定
- detector.add(ASCIIDetector.getInstance());
- // detector.add(UnicodeDetector)
- // UnicodeDetector用于Unicode家族编码的测定
- // detector.add(UnicodeDetector.getInstance());
- // info.monitorenter.cpdetector.io.UnicodeDetector.getInstance();
- java.nio.charset.Charset charset = null;
- try {
- charset = detector.detectCodepage(afile.toURL());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- if (charset != null) {
- // System.out.println(" [" + charset.name() + "] " + afile.getPath()
- // + "\\" + afile.getName());
- if (!charset.name().equals(toEncode)) {
- Map<String, String> resMap = new HashMap<String, String>();
- resMap.put("encodeType", charset.name());
- resMap.put("filePath", afile.getPath());
- resMap.put("fileName", afile.getName());
- fileList.add(resMap);
- }
- } else
- System.out.println(" [?] " + afile.getPath() + "\\" + afile.getName());
- }
- /**
- * 获得文件的类型
- *
- * @param afile
- * @return
- */
- private static String getFileExt(java.io.File afile) {
- String fileName = afile.getName();
- return (fileName.substring(fileName.lastIndexOf(".") + 1)).toLowerCase();
- }
- /**
- * 是否需要检查文件的编码格式
- *
- * @param afile
- * @return
- */
- private static boolean isScan(String fileType) {
- for (int i = 0; i < ScanExtName.length; i++) {
- if (fileType.equals(ScanExtName[i])) {
- return true;
- }
- }
- return false;
- }
- public String getDifferFileList() {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < fileList.size(); i++) {
- Map<String, String> resMap = fileList.get(i);
- String encodeType = resMap.get("encodeType") == null ? "" : resMap.get("encodeType");
- String filePath = resMap.get("filePath") == null ? "" : resMap.get("filePath");
- // String fileName = resMap.get("fileName") == null ? "" :
- // resMap.get("fileName");
- sb.append(" [" + encodeType + "] " + filePath + "<br>");
- }
- return sb.toString();
- }
- // private static String getFileEncode(java.io.File f) {
- //
- // String result = "";
- // try {
- // java.io.InputStream ios = new java.io.FileInputStream(f);
- // byte[] b = new byte[3];
- // ios.read(b);
- // ios.close();
- // if (b[0] == -17 && b[1] == -69 && b[2] == -65){
- // result = "UTF-8";
- // System.out.println(f.getName() + "编码是:" + result);
- // }else {
- // System.out.println(f.getName() + "编码不是:UTF-8");
- // result = "!UTF-8";
- // }
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // return result;
- //
- // }
- }
|