技术点:
1、使用htmlparser解析html文件,得到html代码里的js、css、img链接
2、将相对路径的链接转变成绝对路径,并读取资源
3、将css和js合并到html 、
4、将图片转换成base64编码,写入标签的src
使用的插件
1、htmlparser – 解析html
2、sun.misc.BASE64Encoder – 获取图片的base64编码
package jsCssInHtml;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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;
import Decoder.BASE64Encoder;
public class JsCssImgInHtml {
private URL strWeb = null; //网址
private String strEncoding = null; //编码
private String strText = null; //网页文本
private String strFileName = null; //导出路径
public JsCssImgInHtml(String strText, String strUrl, String strEncoding, String strFileName) {
try {
strWeb = new URL(strUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
return;
}
this.strText = strText;
this.strEncoding = strEncoding;
this.strFileName = strFileName;
}
// 将InputStream转换成按字符编码的String
public static String InputStreamTOString(InputStream in, String encoding) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024 * 1000];
int count = -1;
while ((count = in.read(data, 0, 1024 * 1000)) != -1) {
outStream.write(data,