java 静态页面_java实现生成首页、列表页的静态页面

//生成方法 pathUrl页面访问路径,path静态页面存放的路径,channelPath 主要是来匹配针对栏目列表页

/**

*定时方法调用静态页面创建方法

*/

private boolean createIndexPage(String pathUrl,String path,String channelPath){

BufferedReader bufReader=null;

BufferedWriter bufWriter=null;

try {

String tempTime=ComUtils.formatCurrentDate(6);

String filePath = null;

String tempFileName=tempTime+".tmp";

filePath = path+File.separator+tempFileName;

File dirFile =new File(path);

if(!(dirFile.exists())){

dirFile.mkdirs();

}

String zfj=ENCODE;

boolean flag=false;//记录文件是否写成功

bufReader=PageStaticUtil.readUrlContent(pathUrl);//读取网址的信息

bufWriter = new BufferedWriter( new OutputStreamWriter(new FileOutputStream (filePath),zfj));

if(bufReader!=null){

flag= PageStaticUtil.copy(bufReader, bufWriter);//写文件

bufReader.close();

bufWriter.close();

}

if(flag){

String renameStr=PageStaticUtil.renamePage(path,tempFileName,INDEXPAGENAME);如果文件写成功就重命文名为你想要的文件

if(renameStr.length()>0)//重命名静态页面文件

{

log.info("重命名成功!");

delFile(renameStr);

log.info("删除成功!");

}

}

return true;

} catch (Exception e) {

log.error("Exception", e);

if(bufReader!=null){

try {

bufReader.close();

} catch (IOException e1) {

log.error("IOException", e1);

}

bufReader=null;

}

if(bufWriter!=null){

try {

bufWriter.close();

} catch (IOException e1) {

log.error("IOException", e1);

}

bufWriter=null;

}

return false;

}

}

//读取网址并反回流

public static final String ENCODE="UTF-8";

public static final int TIMEOUT=300*1000;

public static BufferedReader readUrlContent(String pathUrl){

BufferedReader bufReader=null;

try {

String accessUrl="http://" + pathUrl;

//    url =  new URL("http://" + pathUrl+ "/?jumpPage=2");//jumpPage 可以防止不要每次读取是静态面的数据

URL url =  new URL(accessUrl);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setConnectTimeout(TIMEOUT);//设置连接超时,如果网络出现异常,系统会自动重新再执行

connection.setReadTimeout(TIMEOUT);//设置读取数据超时,如果网络出现异常,系统会自动重新再执行

//防止服务器屏蔽java程序作为客户端去防问数据

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

//获取网站编码信息

String zfj=ENCODE;

if(connection.getHeaderField("Content-Type")!=null){

zfj= connection.getHeaderField("Content-Type").substring(connection.getHeaderField("Content-Type").indexOf("charset=") + "charset=".length());

}

bufReader = new BufferedReader(new InputStreamReader(connection.getInputStream(),zfj));

} catch (Exception e) {

e.printStackTrace();

}

return bufReader;

}

/**

* 重命名静态页面名称

* @param filePath 静态页面的完全路径

* @param path 静态面面的路径

* @return boolean

* @throws IOException

*/

public static String renamePage(String path,String oldFileName,String INDEXPAGENAME){

String oldStr="";

String tempTime=ComUtils.formatCurrentDate(6);

File oldFilePath=new File(path+File.separator+oldFileName);//临时首页页面的路径

File newFilePath=new File(path+File.separator+INDEXPAGENAME);//新首面页面的路径

if(oldFilePath.exists()){

boolean flag=true;

if(newFilePath.exists()){//如果新首页页面文件存在,就将文件重命名

flag=false;

oldStr=path+File.separator+tempTime+"old.tmp";

flag=newFilePath.renameTo(new File(oldStr));

System.out.println("重命名首页新文件成功");

}

if(flag){

try {

oldFilePath.renameTo(new File(path+File.separator+INDEXPAGENAME));

System.out.println("重命名首页旧文件成功");

} catch (Exception e) {

e.printStackTrace();

}

}

}

return oldStr;

}

/**

* 将读流数据生成html页面

* @param read 读取数据流对象

* @param write

* @return boolean

* @throws IOException

*/

@SuppressWarnings("unused")

public static  boolean copy(BufferedReader read,BufferedWriter write){

String str;

boolean flag=false;

try {

StringBuilder dataStr = new StringBuilder();

while((str=read.readLine())!=null ){

dataStr.append(str).append("\n");

}

//如果有字符

if(dataStr.length()>0){

write.write(dataStr.toString());

flag=true;

}

} catch (Exception e) {

flag=false;

e.printStackTrace();

}

return flag;

}

代码有点多,如果想生成栏目所有的列表页的静态页面,或有好的建议,请留言

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值