日期:2014-05-16  浏览次数:20374 次

JSP 转换 mht java 代码

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.DefaultParserFeedback;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
/**
* mht文件解析类
* @author dl
*/

public class Html2MHTCompiler {
private URL strWeb = null; /**网页地址*/
private String strText = null; /**网页文本内容*/
private String strFileName = null; /**本地文件名*/
private String strEncoding = null; /**网页编码*/
//mht格式附加信息
private String from = "dongle2001@126.com";
private String to;
private String subject = "mht compile";
private String cc;
private String bcc;
private String smtp = "localhost";

public static void main(String[] args) {
    String strUrl = "http://www.mtime.com/my/tropicofcancer/blog/843555/";
    String strEncoding = "utf-8";
    //String strText = JQuery.getHtmlText(strUrl, strEncoding, null);
    String strText = downFileTxt(strUrl, strEncoding);

   if (strText == null)
     return;

   Html2MHTCompiler h2t = new Html2MHTCompiler(strUrl, strEncoding, "test.mht");
    h2t.compile();
    //Html2MHTCompiler.mht2html("test.mht", "a.html");
}
/**
*
方法说明:初始化
*
输入参数:strText 网页文本内容; strUrl 网页地址; strEncoding 网页编码; strFileName 本地文件名
*
返回类型:
*/
public Html2MHTCompiler(String strUrl, String strEncoding, String strFileName) {
    try {
     strWeb = new URL(strUrl);
    } catch (MalformedURLException e) {
     e.printStackTrace();
     return;
    }
    //this.strText = strText;
    this.strText = downFileTxt(strUrl, strEncoding);
    this.strEncoding = strEncoding;
    this.strFileName = strFileName;
}
/**
*
方法说明:执行下载操作
*
输入参数:
*
返回类型:
*/
public boolean compile() {
    if (strWeb == null || strText == null || strFileName == null || strEncoding == null)
     return false;
    HashMap urlMap = new HashMap();
    NodeList nodes = new NodeList();
    try {
     Parser parser = createParser(strText);
     parser.setEncoding(strEncoding);
     nodes = parser.parse(