实现任何页面静态化源码
2010-05-18 16:08
274 查看
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HomeStaticAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String sUrl = request.getParameter("sUrl"); //需要静态化的页面
String charset = request.getParameter("charset"); //编码
String sHtmlFile = request.getParameter("sHtmlFile");//静态化后页面的名称
String sSavePath = request.getSession().getServletContext().getRealPath("/"+"statichtml"); //静态化后的页面保存的路径
//String sSavePath = "E://cts//huajiao//WebRoot";
//System.out.println("sSavePath........."+sSavePath);
int HttpResult;
URL url = new URL(sUrl);
URLConnection urlconn = url.openConnection();
// 抽象类 URLConnection
// 是所有类的超类,它代表应用程序和 URL
// 之间的通信链接,通过在 URL 上调用
// openConnection 方法创建连接对象
urlconn.connect(); // 使用 connect 方法建立到远程对象的实际连接
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
// 每个
// HttpURLConnection
// 实例都可用于生成单个请求,
// 但是其他实例可以透明地共享连接到
// HTTP 服务器的基础网络
HttpResult = httpconn. getResponseCode();
// getResponseCode可以从 HTTP
// 响应消息获取状态码
if (HttpResult != HttpURLConnection.HTTP_OK) {
} else {
InputStreamReader isr = new InputStreamReader(httpconn. getInputStream(),
charset);
BufferedReader in = new BufferedReader(isr);
//BufferedReader in = new BufferedReader(new UnicodeReader(in, Charset.defaultCharset().name());
String inputLine;
if (!sSavePath.endsWith("/")) {
sSavePath += "//";
}
FileOutputStream fout = new FileOutputStream(sSavePath + sHtmlFile);
while ((inputLine = in.readLine()) != null) {
//System.out.println(inputLine);
fout.write((inputLine+"/n").getBytes());
}
in.close();
fout.close();
}
String templateContent="";
FileInputStream fileinputstream=new FileInputStream(sSavePath+"/" + sHtmlFile);
int length=fileinputstream.available();
byte byts[]=new byte[length];
fileinputstream.read(byts);
fileinputstream.close();
templateContent=new String(byts);
// String a="/"";
// String b=".";
// String c="</jsp:include>";
// String ab="<jsp:include page="+a+"/jsp/index_html/online"+b+"jsp"+a+">"+c;
// templateContent=templateContent.replaceAll("<!--Online-->", ab);
FileOutputStream fileoutputstream=new FileOutputStream(sSavePath + sHtmlFile);
byte outbyte[]=templateContent.getBytes("utf-8");
fileoutputstream.write(outbyte);
fileoutputstream.close();
return mapping.findForward("success_html");
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class HomeStaticAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String sUrl = request.getParameter("sUrl"); //需要静态化的页面
String charset = request.getParameter("charset"); //编码
String sHtmlFile = request.getParameter("sHtmlFile");//静态化后页面的名称
String sSavePath = request.getSession().getServletContext().getRealPath("/"+"statichtml"); //静态化后的页面保存的路径
//String sSavePath = "E://cts//huajiao//WebRoot";
//System.out.println("sSavePath........."+sSavePath);
int HttpResult;
URL url = new URL(sUrl);
URLConnection urlconn = url.openConnection();
// 抽象类 URLConnection
// 是所有类的超类,它代表应用程序和 URL
// 之间的通信链接,通过在 URL 上调用
// openConnection 方法创建连接对象
urlconn.connect(); // 使用 connect 方法建立到远程对象的实际连接
HttpURLConnection httpconn = (HttpURLConnection) urlconn;
// 每个
// HttpURLConnection
// 实例都可用于生成单个请求,
// 但是其他实例可以透明地共享连接到
// HTTP 服务器的基础网络
HttpResult = httpconn. getResponseCode();
// getResponseCode可以从 HTTP
// 响应消息获取状态码
if (HttpResult != HttpURLConnection.HTTP_OK) {
} else {
InputStreamReader isr = new InputStreamReader(httpconn. getInputStream(),
charset);
BufferedReader in = new BufferedReader(isr);
//BufferedReader in = new BufferedReader(new UnicodeReader(in, Charset.defaultCharset().name());
String inputLine;
if (!sSavePath.endsWith("/")) {
sSavePath += "//";
}
FileOutputStream fout = new FileOutputStream(sSavePath + sHtmlFile);
while ((inputLine = in.readLine()) != null) {
//System.out.println(inputLine);
fout.write((inputLine+"/n").getBytes());
}
in.close();
fout.close();
}
String templateContent="";
FileInputStream fileinputstream=new FileInputStream(sSavePath+"/" + sHtmlFile);
int length=fileinputstream.available();
byte byts[]=new byte[length];
fileinputstream.read(byts);
fileinputstream.close();
templateContent=new String(byts);
// String a="/"";
// String b=".";
// String c="</jsp:include>";
// String ab="<jsp:include page="+a+"/jsp/index_html/online"+b+"jsp"+a+">"+c;
// templateContent=templateContent.replaceAll("<!--Online-->", ab);
FileOutputStream fileoutputstream=new FileOutputStream(sSavePath + sHtmlFile);
byte outbyte[]=templateContent.getBytes("utf-8");
fileoutputstream.write(outbyte);
fileoutputstream.close();
return mapping.findForward("success_html");
}
}
相关文章推荐
- ]不需要mod_rewrite直接使用php实现伪静态化页面
- php动态网页实现页面静态化 通过在初次被访问时生成html文件保存起来,下次该PHP程序被访问时就直接找到以前被访问过的html页面
- Asp.Net MVC页面静态化功能实现一:利用IHttpModule和ResultFilter
- 使用RewritePath方法实现【不同路径+任意URL后缀重写到指定页面且URL地址不变】(附源码)_AX
- 利用ResultFilter实现asp.net mvc 页面静态化
- ASP.NET 路由实现页面静态化
- JS禁用页面上所有控件的实现方法(附demo源码下载)
- [原](源码)JS OOP 实现页面浮动图片,支持多图同时浮动,简单调用
- 通过Freemarker实现页面静态化的基本操作
- JSP页面静态化总结之一使用URLRewrite实现url地址伪静态化
- node-express - 页面静态化实现
- ASP.NET MVC使用SSI来实现页面静态化
- Rewrite实现页面静态化重写
- 三,PHP缓存机制实现页面静态化
- 利用php的ob缓存机制实现页面静态化
- php实现页面静态化
- 滑动门和翻转门实现的横竖双tab标签测试页面(附源码)
- PHP如何实现页面静态化
- Freemarker实现页面静态化cms
- 一个简单的用javascript实现的页面内容过滤显示小程序源码