f46ee27a4ed7ed7e6f1846e902eedd3986f8cd62.svn-base 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.toolkit.file.encode.frame.tools;
  2. import java.awt.Image;
  3. import java.awt.image.BufferedImage;
  4. import java.io.BufferedReader;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStreamWriter;
  11. import com.sun.image.codec.jpeg.JPEGCodec;
  12. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  13. public class ConverEncoding{
  14. /**
  15. * 1、修改文件的编码方式,比如原先的编码是big5,我把它转换成UTF-8。
  16. * 2、拷贝图片,读取图片然后把文件放到指定的目录。(这个地方可以扩展,我是用一个图片缩放类
  17. * ,修改过来的,因此这个部分可以修改为具有图片缩放功能的模块)。 3、网页编码方式替换,我读取文件的时候直接把网页指定的编码也给替换成UTF-8了
  18. */
  19. static String strDealObj = "C:\\temp\\javascript";
  20. static String strNewPath = "C:\\temp\\javascriptNew";
  21. static String[] strCanDoExtName = { "jpg", "jpeg", "gif", "png" };
  22. /**
  23. * @param args
  24. * @throws Exception
  25. */
  26. public static void main(String[] args) throws Exception {
  27. File objFile = new File(strDealObj);
  28. doDealWithObj(objFile, strNewPath);
  29. }
  30. private static void doDealWithObj(File objFile, String strNewPath) throws Exception {
  31. if (objFile.isDirectory()) {
  32. File[] objFiles = objFile.listFiles();
  33. for (int i = 0; i < objFiles.length; i++) {
  34. if (objFiles[i].isDirectory()) {
  35. doDealWithObj(objFiles[i], createPathName(strNewPath, objFiles[i].getName()));
  36. } else {
  37. // System.out.println(objFiles[i].getPath());
  38. // System.out.println(strNewPath);
  39. // System.out.println("-------------------");
  40. judgeFile(objFiles[i], strNewPath, "big5", "UTF-8");
  41. }
  42. }
  43. } else {
  44. // System.out.println(objFile.getPath());
  45. // System.out.println(strNewPath);
  46. // System.out.println("================");
  47. judgeFile(objFile, strNewPath, "big5", "UTF-8");
  48. }
  49. }
  50. private static String createPathName(String strPathName, String strFolderName) {
  51. if (strPathName.lastIndexOf("\\") == strPathName.length()) {
  52. return strPathName + strFolderName;
  53. } else {
  54. return strPathName + "\\" + strFolderName;
  55. }
  56. }
  57. private static void judgeFile(File oldFile, String newFilePath, String strOldEncoding,
  58. String strNewEncoding) throws Exception {
  59. File fileNewPath = new File(newFilePath);
  60. if (!fileNewPath.exists()) {
  61. fileNewPath.mkdirs();
  62. }
  63. String strFileName = oldFile.getName();
  64. String strExt = getFileExtension(strFileName);
  65. int intExt = isBelongPic(strExt, strCanDoExtName);
  66. if (intExt != -1) {
  67. copyPic(oldFile, newFilePath, intExt);
  68. } else {
  69. copyFile(oldFile, newFilePath, "big5", "UTF-8");
  70. }
  71. }
  72. private static void copyFile(File oldFile, String newFilePath, String strOldEncoding,
  73. String strNewEncoding) throws Exception {
  74. FileInputStream fileInputStream = null;
  75. InputStreamReader inputStreamRead = null;
  76. BufferedReader bufferRead = null;
  77. BufferedWriter newFileBW = null;
  78. OutputStreamWriter outputStreamWriter = null;
  79. FileOutputStream fileOutputStream = null;
  80. // boolean blnCopyOK = false;
  81. try {
  82. fileInputStream = new FileInputStream(oldFile);
  83. inputStreamRead = new InputStreamReader(fileInputStream, strOldEncoding);
  84. bufferRead = new BufferedReader(inputStreamRead);
  85. File copyFile = new File(createPathName(newFilePath, oldFile.getName()));
  86. fileOutputStream = new FileOutputStream(copyFile, false);
  87. outputStreamWriter = new OutputStreamWriter(fileOutputStream, strNewEncoding);
  88. newFileBW = new BufferedWriter(outputStreamWriter);
  89. String strTSVLine = "";
  90. while ((strTSVLine = bufferRead.readLine()) != null) {
  91. if (strTSVLine.equals("")) {
  92. continue;
  93. }
  94. newFileBW.write(strTSVLine.replaceAll(" charset=big5", " charset=UTF-8") + "\r\n");
  95. }
  96. // blnCopyOK = true;
  97. } finally {
  98. if (bufferRead != null)
  99. bufferRead.close();
  100. // if (blnCopyOK) {
  101. // oldFile.delete();
  102. // }
  103. if (newFileBW != null) {
  104. newFileBW.flush();
  105. newFileBW.close();
  106. }
  107. }
  108. }
  109. private static void copyPic(File oldFile, String newFilePath, int intPicType) throws Exception {
  110. String strFileName;
  111. strFileName = oldFile.getName(); // 獲得文件名
  112. // 獲得文件擴展名
  113. // String strExtType = getFileExtension(strFileName);
  114. // *圖片文件處理
  115. // File imagefile = new File(strFileName); // 讀入文件
  116. Image src_image = javax.imageio.ImageIO.read(oldFile);
  117. // 獲得圖片文件的高度和寬度
  118. int width = src_image.getWidth(null); // 得到源圖寬
  119. int height = src_image.getHeight(null);
  120. // 在申請的圖片緩存區中重繪圖片
  121. BufferedImage buf_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  122. // 繪制縮小后的圖
  123. buf_image.getGraphics().drawImage(src_image, 0, 0, width, height, null);
  124. // 重新建立文件輸出流,并編碼
  125. FileOutputStream out = new FileOutputStream(createPathName(newFilePath, strFileName));
  126. // 輸出到文件流
  127. JPEGImageEncoder jpg_encoder = JPEGCodec.createJPEGEncoder(out);
  128. jpg_encoder.encode(buf_image);
  129. // 使用JPEG編碼文件
  130. // 關閉文件流
  131. out.close();
  132. }
  133. public static String getFileExtension(String fileName) {
  134. return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
  135. }
  136. private static int isBelongPic(String strFileExt, String[] strCanDoPic) {
  137. for (int i = 0; i < strCanDoPic.length; i++) {
  138. if (strFileExt.equalsIgnoreCase(strCanDoPic[i])) {
  139. return i;
  140. }
  141. }
  142. return -1;
  143. }
  144. }