根据 url生成html 静态化

package com.mjp.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;


/**将根据url生成静态的html
* @author mojianpo
*
*/
public class MakeHtml {
private static long star = 0;
private static long end = 0;
private static long ttime = 0;

private static final Logger logger = Logger.getLogger(MakeHtml.class.getName());
/**
* 根据url 生成对应的 html代码
*
* @param httpUrl
* @return
*/
public static String getHtmlCode(String httpUrl) {
httpUrl = StringUtil.nvl(httpUrl);
if(httpUrl.equals("")){
return "";
}
Date before = new Date();
star = before.getTime();
String htmlCode = "";
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String currentLine;
while ((currentLine = breader.readLine()) != null) {
htmlCode += currentLine + "\n";
}
} catch (Exception e) {
logger.error(e.getCause());
e.printStackTrace();
} finally {
Date after = new Date();
end = after.getTime();
ttime = end - star;


logger.debug("执行时间:" + ttime + "秒");
}
return htmlCode;
}

/**
* 根据给的的url List 生成给定名称路径的html文件列表
*
* @param urlLst
* url列表
* @param fileLst
* 要生成的文件的全路径列表
* @param isDel
* true 表示 要删除存在的文件
*/
public static synchronized void writeHtml(List<String> urlLst, List<String> fileLst,
boolean isDel) {
if(urlLst == null || fileLst == null ||(urlLst.size() != fileLst.size())){
return;
}
for (int i = 0,count = urlLst.size();i < count; i++) {
writeHtml(urlLst.get(i), fileLst.get(i),isDel);
}

}

/**
* 根据给的的url 生成给定名称路径的html文件
*
* @param url
*
* @param filePath
* @param isDel
* true 表示 要删除存在的文件
*/
public static synchronized void writeHtml(String url, String filePath,
boolean isDel) {
Writer ow = null;
try {
File writeFile = new File(filePath);
boolean isExit = writeFile.exists();
File folder = writeFile.getParentFile();
if(folder.exists() == false){
folder.mkdirs();
}
if (isExit != true) {
writeFile.createNewFile();
} else {
if (isDel) {
writeFile.delete();
writeFile.createNewFile();
}
}
ow = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
ow.write(getHtmlCode(url));
} catch (Exception ex) {
logger.debug(ex.getMessage());
} finally {
try {
ow.close();
} catch (Exception ex) {
logger.debug(ex.getMessage());
}
logger.debug(url + "\n" + filePath + "\n is make html ok..................");
}
}

/**测试main函数
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String httpUrl = "http://localhost:4848/";
//writeHtml(httpUrl, "f:/a.html", true);
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
System.out.println(connection.getResponseCode());
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值