1 根据url获取html文件
public static String getHtmlFile(String fileUrl) {
System.out.println("--- file url: " + fileUrl);
Date before = new Date();
Long start = before.getTime();
StringBuffer htmlCode = new StringBuffer();
try {
InputStream in;
URL url = new java.net.URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.setRequestProperty("Content-type", "text/html");
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.connect();
in = connection.getInputStream();
BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode.append(currentLine + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Date after = new Date();
Long end = after.getTime();
Long elapse = end - start;
System.out.println("Download HTML file time :" + elapse + " ms");
}
return htmlCode.toString();
}
2 放入到指定的位置
public static synchronized void writeHtml(String filePath, String info, String prefix, String flag) {
PrintWriter pw = null;
try {
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
if (isExit != true) {
writeFile.createNewFile();
} else {
if (!flag.equals("NO")) {
writeFile.delete();
writeFile.createNewFile();
}
}
// pw = new PrintWriter(new FileOutputStream(filePath, true));
OutputStreamWriter outstream = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
pw = new PrintWriter(outstream);
String finalBody = processHtmlBody(prefix, info);
pw.println(finalBody);
pw.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
pw.close();
}
}
3 根据需要设置输出内容
private static String processHtmlBody(String prefix, String body) {
// change link path
Pattern patternImg = Pattern.compile("<img\\s+src\\s*=\\s*\"\\s*/", Pattern.DOTALL);
Matcher matcher = patternImg.matcher(body);
String finalBody = matcher.replaceAll("<img src=" + prefix);
Pattern patternScript = Pattern.compile("<script\\s+src\\s*=\\s*\"\\s*/", Pattern.DOTALL);
matcher = patternScript.matcher(finalBody);
finalBody = matcher.replaceAll("<script src=" + prefix);
Pattern patternLink = Pattern.compile("<link\\s+href\\s*=\\s*\"\\s*/", Pattern.DOTALL);
matcher = patternLink.matcher(finalBody);
finalBody = matcher.replaceAll("<link href=" + prefix);
return finalBody;
}
4 测试
public static void main(String[] args) {
String url = "http://www.baidu.com";
writeHtml("C:/Users/wang_/Desktop/test/test.html", getHtmlFile(url), null, "NO");
}
5 小结
一般需要做导出此url的pdf或者图片,先将此url转为html文件,然后再转为pdf或者image。后续会写一个html转pdf或者image的方法