자바 mht 를 html 로 변환
9055 단어 자바
필요 한 Jar 맨 아래 에서 다운로드 할 수 있 습 니 다.
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Enumeration;
import javax.activation.DataHandler;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePartDataSource;
public class HtmlApplication{
public static void main(String[] args){
HtmlApplication.mht2html("C:\\Documents and Settings\\Administrator\\ \\test2.mht", "C:\\test2\\test.html");
}
/**
* mht html
* @param s_SrcMht
* @param s_DescHtml
*/
public static void mht2html(String s_SrcMht, String s_DescHtml) {
try {
InputStream fis = new FileInputStream(s_SrcMht);
Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
MimeMessage msg = new MimeMessage(mailSession, fis);
Object content = msg.getContent();
if (content instanceof Multipart){
MimeMultipart mp = (MimeMultipart)content;
MimeBodyPart bp1 = (MimeBodyPart)mp.getBodyPart(0);
// mht
String strEncodng = getEncoding(bp1);
// mht
String strText = getHtmlText(bp1, strEncodng);
if (strText == null)
return;
// mht , 。
File parent = null;
if (mp.getCount() > 1) {
parent = new File(new File(s_DescHtml).getAbsolutePath() + ".files");
parent.mkdirs();
if (!parent.exists()){ //
return;
}
}
//FOR
for (int i = 1; i < mp.getCount(); ++i) {
MimeBodyPart bp = (MimeBodyPart)mp.getBodyPart(i);
//
// ( : http://xxx.com/abc.jpg)
String strUrl = getResourcesUrl(bp);
if (strUrl==null || strUrl.length()==0)
continue;
DataHandler dataHandler = bp.getDataHandler();
MimePartDataSource source = (MimePartDataSource)dataHandler.getDataSource();
//
String FilePath = parent.getAbsolutePath() + File.separator + getName(strUrl, i);
File resources = new File(FilePath);
//
if (SaveResourcesFile(resources, bp.getInputStream())){
// 、JS、CSS
strText = strText.replace(strUrl, resources.getAbsolutePath());
}
}
// HTML
SaveHtml(strText, s_DescHtml, strEncodng);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* mht
* @param strName
* @param ID
* @return
*/
public static String getName(String strName, int ID) {
char separator1 = '/';
char separator2 = '\\';
//
strName = strName.replaceAll("\r
", "");
//
if( strName.lastIndexOf(separator1) >= 0){
return strName.substring(strName.lastIndexOf(separator1) + 1);
}
if( strName.lastIndexOf(separator2) >= 0){
return strName.substring(strName.lastIndexOf(separator2) + 1);
}
return "";
}
/**
* html 。
* @param strText
* @param strHtml
* @param strEncodng
*/
public static boolean SaveHtml(String s_HtmlTxt, String s_HtmlPath , String s_Encode) {
try{
Writer out = null;
out = new OutputStreamWriter(new FileOutputStream(s_HtmlPath, false), s_Encode);
out.write(s_HtmlTxt);
out.close();
}catch(Exception e){
return false;
}
return true;
}
/**
* JS、 、CSS
* @param SrcFile
* @param inputStream
* @return
*/
private static boolean SaveResourcesFile(File SrcFile, InputStream inputStream) {
if (SrcFile == null || inputStream == null) {
return false;
}
BufferedInputStream in = null;
FileOutputStream fio = null;
BufferedOutputStream osw = null;
try {
in = new BufferedInputStream(inputStream);
fio = new FileOutputStream(SrcFile);
osw = new BufferedOutputStream(new DataOutputStream(fio));
int index = 0;
byte[] a = new byte[1024];
while ((index = in.read(a)) != -1) {
osw.write(a, 0, index);
}
osw.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally{
try {
if (osw != null)
osw.close();
if (fio != null)
fio.close();
if (in != null)
in.close();
if (inputStream != null)
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
/**
* mht URL
* @param bp
* @return
*/
private static String getResourcesUrl(MimeBodyPart bp) {
if(bp==null){
return null;
}
try {
Enumeration list = bp.getAllHeaders();
while (list.hasMoreElements()) {
javax.mail.Header head = (javax.mail.Header)list.nextElement();
if (head.getName().compareTo("Content-Location") == 0) {
return head.getValue();
}
}
return null;
} catch (MessagingException e) {
return null;
}
}
/**
* mht
* @param bp
* @param strEncoding mht
* @return
*/
private static String getHtmlText(MimeBodyPart bp, String strEncoding) {
InputStream textStream = null;
BufferedInputStream buff = null;
BufferedReader br = null;
Reader r = null;
try {
textStream = bp.getInputStream();
buff = new BufferedInputStream(textStream);
r = new InputStreamReader(buff, strEncoding);
br = new BufferedReader(r);
StringBuffer strHtml = new StringBuffer("");
String strLine = null;
while ((strLine = br.readLine()) != null) {
strHtml.append(strLine + "\r
");
}
br.close();
r.close();
textStream.close();
return strHtml.toString();
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
if (br != null)
br.close();
if (buff != null)
buff.close();
if (textStream != null)
textStream.close();
}catch(Exception e){
}
}
return null;
}
/**
* mht
* @param bp
* @return
*/
private static String getEncoding(MimeBodyPart bp) {
if(bp==null){
return null;
}
try {
Enumeration list = bp.getAllHeaders();
while (list.hasMoreElements()) {
javax.mail.Header head = (javax.mail.Header)list.nextElement();
if (head.getName().compareTo("Content-Type") == 0) {
String strType = head.getValue();
int pos = strType.indexOf("charset=");
if (pos>=0) {
String strEncoding = strType.substring(pos + 8, strType.length());
if(strEncoding.startsWith("\"") || strEncoding.startsWith("\'")){
strEncoding = strEncoding.substring(1 , strEncoding.length());
}
if(strEncoding.endsWith("\"") || strEncoding.endsWith("\'")){
strEncoding = strEncoding.substring(0 , strEncoding.length()-1);
}
if (strEncoding.toLowerCase().compareTo("gb2312") == 0) {
strEncoding = "gbk";
}
return strEncoding;
}
}
}
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.