网站html静态页生成办法

编程

一、获取商品列表,设置路径

@RequestMapping("/createAllGoodsHtml")

public void createAllGoodsHtml() throws Exception{

System.out.print("手动生成全部商品----->>>");

List<Long> goodsInfoIdlist=goodsInfoNServiceImpl.queryAllGoodsInfoId();

if(CollectionUtils.isNotEmpty(goodsInfoIdlist)){

String pcPath=CreateHtmlUtil.FILEPATH+"/pc/goods/";//添加文件夹用于区分功能

String wapPath= CreateHtmlUtil.FILEPATH+"/wap/goods/";//添加文件夹用于区分功能

File pcfile=new File(pcPath);

if(!pcfile.exists()){

pcfile.mkdirs();

}

File wapfile=new File(wapPath);

if(!wapfile.exists()){

wapfile.mkdirs();

}

// System.out.println("商品静态化完成!!!");

CreateHtmlThread createHtmlThread=new CreateHtmlThread(1,goodsInfoIdlist);

int threadCount=CreateHtmlUtil.THREAD_COUNT;

for(int i=0;i<threadCount;i++){

new Thread(createHtmlThread).start();

}

}

}

二、工具类编写

public class CreateHtmlThread implements Runnable {

private int type;//1商品 2文章 3问答

private List<Long> longList= Collections.synchronizedList(new ArrayList<Long>());

public CreateHtmlThread(int t, List<Long> list){

this.type=t;

this.longList=list;

}

@Override

public void run() {

while(longList.size()>0){

Long id=longList.get(longList.size()-1);

if(id!=null) {

longList.remove(id);

}

if(type==1){

createGoodsById(id);

}else if(type==2){

createArticleById(id);

}else if(type==3){

createAskById(id);

}

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public void createGoodsById(Long goodsInfoId){

String pcUrl=CreateHtmlUtil.PC_GOODS_URL+goodsInfoId+".html";

String pcPath1=CreateHtmlUtil.FILEPATH+"/pc/goods/"+"goods-10000"+goodsInfoId+".html";//生成地址

CreateHtmlUtil.createStaticPage(pcUrl,pcPath1,goodsInfoId);//生成PC商品静态页

String wapUrl=CreateHtmlUtil.WAP_GOODS_URL+goodsInfoId+".html";

String wapPath1=CreateHtmlUtil.FILEPATH+"/wap/goods/"+"goods-"+goodsInfoId+".html";//生成地址

CreateHtmlUtil.createStaticPage(wapUrl,wapPath1,goodsInfoId);//生成WAP商品静态页

}

}

三、工具类编写二

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

public class CreateHtmlUtil {

public final static String PC_GOODS_URL="http://pc.localhost/goods-s-10000";//PC商品页面

public final static String WAP_GOODS_URL="http://m.localhost/goods-s-";//WAP商品页面

public final static String PC_ARTICLE_URL="http://pc.localhost/zixun/show-s-";

public final static String WAP_ARTICLE_URL="http://m.localhost/zixun-s-";

public final static String PC_ASK_URL="http://pc.localhost/ask/question-s-";

public final static String WAP_ASK_URL="http://m.localhost/ask-s-";

public final static String FILEPATH="/data/cache";

public final static int THREAD_COUNT=12;//线程总数

public final static String XIAONENG="40827,35807,40563,35820";//需要替换小能的商品

public final static String XIAONENG_KF_OLD="kf_9704_1413527196072";//替换前小能组

public final static String XIAONENG_KF_NEW="kf_9704_1486619114220";//替换后小能组

/**

* 根据网址读取网页HTML内容

* @param pageUrl 网页地址

*/

public static String readerPageByUrl(String pageUrl,String filePath) throws Exception {

URL url = new URL(pageUrl);

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

int state=connection.getResponseCode();

StringBuffer sb = new StringBuffer();

if(state==200) {

InputStream is = connection.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));

String line = null;

while ((line = br.readLine()) != null) {

sb.append(line).append("

");

}

}else{

File file = new File(CreateHtmlUtil.FILEPATH + "/errorlog.txt");

FileOutputStream fileout = new FileOutputStream(file,true);

StringBuffer str = new StringBuffer();

str.append(filePath).append("

");

fileout.write(str.toString().getBytes());

fileout.flush();

fileout.close();

}

return sb.toString();

}

/**

* 写入操作

* @param filePath 静态页面路径

* @param fileStr 网页内容

* @throws Exception

*/

public static void writeStringToFile(String filePath, String fileStr) throws Exception {

File file=new File(filePath);

FileOutputStream fileout = new FileOutputStream(file);

fileout.write(fileStr.getBytes());

fileout.flush();

fileout.close();

}

/**

* 生成静态页面

* @param pageUrl 网址

* @param filePath 静态页面路径

* @throws Exception

*/

public static void createStaticPage(String pageUrl,String filePath) {

try{//获取网页内容

String pageStr=readerPageByUrl(pageUrl,filePath);

writeStringToFile(filePath, pageStr);

}catch(Exception e){

e.printStackTrace();

}

}

/**

* 生成静态页面

* @param pageUrl 网址

* @param filePath 静态页面路径

* @throws Exception

*/

public static void createStaticPage(String pageUrl,String filePath,Long goodsInfoId) {

try{//获取网页内容

String pageStr=readerPageByUrl(pageUrl,filePath);

if(XIAONENG.indexOf(goodsInfoId.toString())!=-1){//商品详情页如果存在需要修改小能客服

pageStr=pageStr.replace(XIAONENG_KF_OLD,XIAONENG_KF_NEW);

}

writeStringToFile(filePath, pageStr);

}catch(Exception e){

e.printStackTrace();

}

}

}

四、通过linux开源工具 sersync2 将生成的文件实时同步至对应nginx的linux服务器中

以上是 网站html静态页生成办法 的全部内容, 来源链接: utcz.com/z/517888.html

回到顶部