Use Java to generate static web page samples from dynamic web pages

  • 2020-04-01 03:12:36
  • OfStack


package com.tools;
import java.io.*;
import java.net.URL;

public class GoToHtml {
/**
 * 
 * @param page
 *             Local file path for static pages ( c,d,e,f,g ) 
 * @param url_addr
 *             Of the static page to be generated URL address (http://)
 * @return
 */
public boolean PrintPage(String page, String url_addr) {
System.out.println("page" + page);
String newPage = "";
//Determines whether the local path of the input is. JSP at the end of the
if (page.endsWith(".html")) {
System.out.println("this is end with xxx.html");
int bias = page.lastIndexOf("/");//Bias gets the position of the last slash
System.out.println("the last / at :" + bias);
newPage = page.substring(0, bias);
System.out.println("newPage:" + newPage);
}
//If the folder does not exist, create one
File ff = new File(newPage);
ff.mkdirs();
URL url;
String rLine = null;
PrintWriter fileOut = null;
InputStream ins = null;
try {
url = new URL(url_addr);
System.out.println(url+".......");
ins = url.openStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(
ins, "utf-8"));//Gets the file coded gb2312
FileOutputStream out = new FileOutputStream(page);
OutputStreamWriter writer = new OutputStreamWriter(out, "utf-8");
fileOut = new PrintWriter(writer);
//Loop to fetch data and write to the target file
while ((rLine = bReader.readLine()) != null) {
String tmp_rLine = rLine;
System.out.println(tmp_rLine);
int str_len = tmp_rLine.length();
if (str_len > 0) {
fileOut.println(tmp_rLine);
fileOut.flush();
}
tmp_rLine = null;
}
url = null;
return true;
} catch (IOException e) {
System.out.println("error: " + e.getMessage());
e.printStackTrace();
return false;
} catch (Exception es) {
System.out.println(es.getMessage());
return false;
} finally {//Close the resource
fileOut.close();
try {
ins.close();
} catch (IOException ex) {
//Close input outflow error
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
GoToHtml gth = new GoToHtml();
String accresstoken="W67K0vH4k5YpH3DpK75JkmEchvW_utYBTqjd-vb8VXRFD3AovHhK_ApJQ7JMouhIytsRU3_VOJ2iXrCcLUAb_6-HblSmXzz_dwqTuP3908aOIoukKRNxLLaGh0aC7rnvsazyByXwi8670us-RYD1vA";
String media="Y9kK2wmpS4byzxIyFz7_NQ6gyi8Ev7-hyZG5HbaDKOqOBLfs9JIt8_x2S4zoVc1g";
String urlString="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+accresstoken+"&media_id="+media;
//gth.PrintPage("D:/eclipse/newNum.html","http://localhost/prime/user/userFind_update.jsp?id=93");
gth.PrintPage("D:/newNum.html",urlString);
}
}


Related articles: