1a0217fb638560a45e670ba78944156eddec9f6f.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.toolkit.file.encode;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import info.monitorenter.cpdetector.io.*;
  7. public class GetfilesEncode{
  8. /**
  9. * 需要检查的文件类型
  10. */
  11. static String[] ScanExtName = { "jsp", "html", "js", "xml", "java",
  12. "json", "css" };
  13. private String toEncode = "";
  14. private ArrayList<Map<String, String>> fileList = new ArrayList<Map<String, String>>();
  15. public GetfilesEncode(String fileForder, String toEncode) {
  16. this.toEncode = toEncode;
  17. File objFile = new File(fileForder);
  18. try {
  19. doDealWithObj(objFile);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. private void doDealWithObj(File objFile) throws Exception {
  25. if (objFile.isDirectory()) {
  26. File[] objFiles = objFile.listFiles();
  27. for (int i = 0; i < objFiles.length; i++) {
  28. if (objFiles[i].isDirectory()) {
  29. doDealWithObj(objFiles[i]);
  30. } else {
  31. // getFileEncode(objFiles[i]);
  32. if (isScan(getFileExt(objFiles[i]))) {
  33. getFileEncodeByDetector(objFiles[i]);
  34. }
  35. }
  36. }
  37. } else {
  38. if (isScan(getFileExt(objFile))) {
  39. getFileEncodeByDetector(objFile);
  40. }
  41. }
  42. }
  43. /**
  44. * 获得文件的编码格式类型
  45. *
  46. * @param afile
  47. */
  48. @SuppressWarnings("deprecation")
  49. private void getFileEncodeByDetector(java.io.File afile) {
  50. CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); // A
  51. // singleton.
  52. /*-------------------------------------------------------------------------
  53. ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于
  54. 指示是否显示探测过程的详细信息,为false不显示。
  55. ---------------------------------------------------------------------------*/
  56. detector.add(new ParsingDetector(false));
  57. detector.add(new ParsingDetector(true)); // be verbose about parsing.
  58. detector.add(new ByteOrderMarkDetector());
  59. /*--------------------------------------------------------------------------
  60. JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码
  61. 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以
  62. 再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
  63. ---------------------------------------------------------------------------*/
  64. detector.add(JChardetFacade.getInstance());
  65. // ASCIIDetector用于ASCII编码测定
  66. detector.add(ASCIIDetector.getInstance());
  67. // detector.add(UnicodeDetector)
  68. // UnicodeDetector用于Unicode家族编码的测定
  69. // detector.add(UnicodeDetector.getInstance());
  70. // info.monitorenter.cpdetector.io.UnicodeDetector.getInstance();
  71. java.nio.charset.Charset charset = null;
  72. try {
  73. charset = detector.detectCodepage(afile.toURL());
  74. } catch (Exception ex) {
  75. ex.printStackTrace();
  76. }
  77. if (charset != null) {
  78. // System.out.println(" [" + charset.name() + "] " + afile.getPath()
  79. // + "\\" + afile.getName());
  80. if (!charset.name().equals(toEncode)) {
  81. Map<String, String> resMap = new HashMap<String, String>();
  82. resMap.put("encodeType", charset.name());
  83. resMap.put("filePath", afile.getPath());
  84. resMap.put("fileName", afile.getName());
  85. fileList.add(resMap);
  86. }
  87. } else
  88. System.out.println(" [?] " + afile.getPath() + "\\" + afile.getName());
  89. }
  90. /**
  91. * 获得文件的类型
  92. *
  93. * @param afile
  94. * @return
  95. */
  96. private static String getFileExt(java.io.File afile) {
  97. String fileName = afile.getName();
  98. return (fileName.substring(fileName.lastIndexOf(".") + 1)).toLowerCase();
  99. }
  100. /**
  101. * 是否需要检查文件的编码格式
  102. *
  103. * @param afile
  104. * @return
  105. */
  106. private static boolean isScan(String fileType) {
  107. for (int i = 0; i < ScanExtName.length; i++) {
  108. if (fileType.equals(ScanExtName[i])) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. public String getDifferFileList() {
  115. StringBuffer sb = new StringBuffer();
  116. for (int i = 0; i < fileList.size(); i++) {
  117. Map<String, String> resMap = fileList.get(i);
  118. String encodeType = resMap.get("encodeType") == null ? "" : resMap.get("encodeType");
  119. String filePath = resMap.get("filePath") == null ? "" : resMap.get("filePath");
  120. // String fileName = resMap.get("fileName") == null ? "" :
  121. // resMap.get("fileName");
  122. sb.append(" [" + encodeType + "] " + filePath + "<br>");
  123. }
  124. return sb.toString();
  125. }
  126. // private static String getFileEncode(java.io.File f) {
  127. //
  128. // String result = "";
  129. // try {
  130. // java.io.InputStream ios = new java.io.FileInputStream(f);
  131. // byte[] b = new byte[3];
  132. // ios.read(b);
  133. // ios.close();
  134. // if (b[0] == -17 && b[1] == -69 && b[2] == -65){
  135. // result = "UTF-8";
  136. // System.out.println(f.getName() + "编码是:" + result);
  137. // }else {
  138. // System.out.println(f.getName() + "编码不是:UTF-8");
  139. // result = "!UTF-8";
  140. // }
  141. // } catch (Exception e) {
  142. // e.printStackTrace();
  143. // }
  144. // return result;
  145. //
  146. // }
  147. }