分享

【JAVA】mht文件转html

 hehffyy 2014-02-20

网上搜索获得mht文件转html,稍作了修改,即可使用,在此做个笔记。

  1. public class Mht2HtmlUtil {
  2. public static void main(String[] args) {
  3. Mht2HtmlUtil.mht2html("d:\\51job_111.mht", "d:\\test.htm");
  4. }
  5. /**
  6. * 将 mht文件转换成 html文件
  7. *
  8. * @param s_SrcMht
  9. * @param s_DescHtml
  10. */
  11. public static void mht2html(String s_SrcMht, String s_DescHtml) {
  12. try {
  13. InputStream fis = new FileInputStream(s_SrcMht);
  14. Session mailSession = Session.getDefaultInstance(
  15. System.getProperties(), null);
  16. MimeMessage msg = new MimeMessage(mailSession, fis);
  17. Object content = msg.getContent();
  18. if (content instanceof Multipart) {
  19. MimeMultipart mp = (MimeMultipart) content;
  20. MimeBodyPart bp1 = (MimeBodyPart) mp.getBodyPart(0);
  21.  
  22. // 获取mht文件内容代码的编码
  23. String strEncodng = getEncoding(bp1);
  24.  
  25. // 获取mht文件的内容
  26. String strText = getHtmlText(bp1, strEncodng);
  27. if (strText == null)
  28. return;
  29.  
  30. // 创建以mht文件名称的文件夹,主要用来保存资源文件。
  31. File parent = null;
  32. if (mp.getCount() > 1) {
  33. parent = new File(new File(s_DescHtml).getAbsolutePath()
  34. + ".files");
  35. parent.mkdirs();
  36. if (!parent.exists()) { // 创建文件夹失败的话则退出
  37. return;
  38. }
  39. }
  40.  
  41. // FOR中代码 主要是保存资源文件及替换路径
  42. for (int i = 1; i < mp.getCount(); ++i) {
  43. MimeBodyPart bp = (MimeBodyPart) mp.getBodyPart(i);
  44. // 获取资源文件的路径
  45. // 例(获取: http:///abc.jpg)
  46. String strUrl = getResourcesUrl(bp);
  47. if (strUrl == null || strUrl.length() == 0)
  48. continue;
  49.  
  50. DataHandler dataHandler = bp.getDataHandler();
  51. MimePartDataSource source = (MimePartDataSource) dataHandler
  52. .getDataSource();
  53.  
  54. // 获取资源文件的绝对路径
  55. String FilePath = parent.getAbsolutePath() + File.separator
  56. + getName(strUrl, i);
  57. File resources = new File(FilePath);
  58.  
  59. // 保存资源文件
  60. if (SaveResourcesFile(resources, bp.getInputStream())) {
  61. // 将远程地址替换为本地地址 如图片、JS、CSS样式等等
  62. strText = strText.replace(strUrl,
  63. resources.getAbsolutePath());
  64. }
  65. }
  66.  
  67. // 最后保存HTML文件
  68. SaveHtml(strText, s_DescHtml, strEncodng);
  69. }
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74.  
  75. /**
  76. * 获取mht文件内容中资源文件的名称
  77. *
  78. * @param strName
  79. * @param ID
  80. * @return
  81. */
  82. public static String getName(String strName, int ID) {
  83. char separator1 = '/';
  84. char separator2 = '\\';
  85. // 将换行替换
  86. strName = strName.replaceAll("\r\n", "");
  87.  
  88. // 获取文件名称
  89. if (strName.lastIndexOf(separator1) >= 0) {
  90. return strName.substring(strName.lastIndexOf(separator1) + 1);
  91. }
  92. if (strName.lastIndexOf(separator2) >= 0) {
  93. return strName.substring(strName.lastIndexOf(separator2) + 1);
  94. }
  95. return "";
  96. }
  97.  
  98. /**
  99. * 将提取出来的html内容写入保存的路径中。
  100. *
  101. * @param strText
  102. * @param strHtml
  103. * @param strEncodng
  104. */
  105. public static boolean SaveHtml(String s_HtmlTxt, String s_HtmlPath,
  106. String s_Encode) {
  107. try {
  108. Writer out = null;
  109. out = new OutputStreamWriter(
  110. new FileOutputStream(s_HtmlPath, false), s_Encode);
  111. out.write(s_HtmlTxt);
  112. out.close();
  113. } catch (Exception e) {
  114. return false;
  115. }
  116. return true;
  117. }
  118.  
  119. /**
  120. * 保存网页中的JS、图片、CSS样式等资源文件
  121. *
  122. * @param SrcFile
  123. * 源文件
  124. * @param inputStream
  125. * 输入流
  126. * @return
  127. */
  128. private static boolean SaveResourcesFile(File SrcFile,
  129. InputStream inputStream) {
  130. if (SrcFile == null || inputStream == null) {
  131. return false;
  132. }
  133.  
  134. BufferedInputStream in = null;
  135. FileOutputStream fio = null;
  136. BufferedOutputStream osw = null;
  137. try {
  138. in = new BufferedInputStream(inputStream);
  139. fio = new FileOutputStream(SrcFile);
  140. osw = new BufferedOutputStream(new DataOutputStream(fio));
  141. int index = 0;
  142. byte[] a = new byte[1024];
  143. while ((index = in.read(a)) != -1) {
  144. osw.write(a, 0, index);
  145. }
  146. osw.flush();
  147. return true;
  148. } catch (Exception e) {
  149. e.printStackTrace();
  150. return false;
  151. } finally {
  152. try {
  153. if (osw != null)
  154. osw.close();
  155. if (fio != null)
  156. fio.close();
  157. if (in != null)
  158. in.close();
  159. if (inputStream != null)
  160. inputStream.close();
  161. } catch (Exception e) {
  162. e.printStackTrace();
  163. return false;
  164. }
  165. }
  166. }
  167.  
  168. /**
  169. * 获取mht文件里资源文件的URL路径
  170. *
  171. * @param bp
  172. * @return
  173. */
  174. private static String getResourcesUrl(MimeBodyPart bp) {
  175. if (bp == null) {
  176. return null;
  177. }
  178. try {
  179. Enumeration list = bp.getAllHeaders();
  180. while (list.hasMoreElements()) {
  181. javax.mail.Header head = (javax.mail.Header) list.nextElement();
  182. if (head.getName().compareTo("Content-Location") == 0) {
  183. return head.getValue();
  184. }
  185. }
  186. return null;
  187. } catch (MessagingException e) {
  188. return null;
  189. }
  190. }
  191.  
  192. /**
  193. * 获取mht文件中的内容代码
  194. *
  195. * @param bp
  196. * @param strEncoding
  197. * 该mht文件的编码
  198. * @return
  199. */
  200. private static String getHtmlText(MimeBodyPart bp, String strEncoding) {
  201. InputStream textStream = null;
  202. BufferedInputStream buff = null;
  203. BufferedReader br = null;
  204. Reader r = null;
  205. try {
  206. textStream = bp.getInputStream();
  207. buff = new BufferedInputStream(textStream);
  208. r = new InputStreamReader(buff, strEncoding);
  209. br = new BufferedReader(r);
  210. StringBuffer strHtml = new StringBuffer("");
  211. String strLine = null;
  212. while ((strLine = br.readLine()) != null) {
  213. System.out.println(strLine);
  214. strHtml.append(strLine + "\r\n");
  215. }
  216. br.close();
  217. r.close();
  218. textStream.close();
  219. return strHtml.toString();
  220. } catch (Exception e) {
  221. e.printStackTrace();
  222. } finally {
  223. try {
  224. if (br != null)
  225. br.close();
  226. if (buff != null)
  227. buff.close();
  228. if (textStream != null)
  229. textStream.close();
  230. } catch (Exception e) {
  231. }
  232. }
  233. return null;
  234. }
  235.  
  236. /**
  237. * 获取mht网页文件中内容代码的编码
  238. *
  239. * @param bp
  240. * @return
  241. */
  242. private static String getEncoding(MimeBodyPart bp) {
  243. if (bp == null) {
  244. return null;
  245. }
  246. try {
  247. Enumeration list = bp.getAllHeaders();
  248. while (list.hasMoreElements()) {
  249. javax.mail.Header head = (javax.mail.Header) list.nextElement();
  250. if (head.getName().equalsIgnoreCase("Content-Type")) {
  251. String strType = head.getValue();
  252. int pos = strType.indexOf("charset=");
  253. if (pos >= 0) {
  254. String strEncoding = strType.substring(pos + 8,
  255. strType.length());
  256. if (strEncoding.startsWith("\"")
  257. || strEncoding.startsWith("\'")) {
  258. strEncoding = strEncoding.substring(1,
  259. strEncoding.length());
  260. }
  261. if (strEncoding.endsWith("\"")
  262. || strEncoding.endsWith("\'")) {
  263. strEncoding = strEncoding.substring(0,
  264. strEncoding.length() - 1);
  265. }
  266. if (strEncoding.toLowerCase().compareTo("gb2312") == 0) {
  267. strEncoding = "gbk";
  268. }
  269. return strEncoding;
  270. }
  271. }
  272. }
  273. } catch (MessagingException e) {
  274. e.printStackTrace();
  275. }
  276. return null;
  277. }
  278. }

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约