注: 若需要被静态化的 页面中 使用了 response.sendRedirect 跳转, 则最后静态页面为 最终跳转后的页面.
而那些 使用 JS 跳转的 比如 Windows.location.href 则 无效, 直接作为 JS 代码在生成的静态页面中, 并执行.
- // 保存为文件
- import java.io.*;
- public class GenerateIndexPage {
- protected static String defaultToFile = "frame/main_Null.html";
- protected static String defaultFromFile = "http://localhost:8080/stfb/frame/main_Null.jsp";
- String result = "";
- public String genHtml(String fromFile,String toFile) throws Exception {
- if("".equals(fromFile)||fromFile==null){
- fromFile = defaultFromFile;
- }
- if("".equals(toFile)||toFile==null){
- toFile = defaultToFile;
- }
- java.NET.URL url = new java.NET.URL(fromFile);
- java.NET.HttpURLConnection conn =(java.NET.HttpURLConnection) url.openConnection();
- try{
- if (conn.getResponseCode() == 200) {
- java.io.InputStream is = (java.io.InputStream) conn.getContent();
- try{
- ConfigInfo cfn = new ConfigInfo();
- String server_path = cfn.getValue("server_path");
- String savePath = server_path+"\\"+toFile;
- FileOutputStream baos = new FileOutputStream(new File(savePath));
- int buffer = 1024;
- byte[] b = new byte[buffer];
- int n = 0;
- while ((n = is.read(b, 0, buffer))> 0) {
- baos.write(b, 0, n);
- }
- //String s = new String(baos.toByteArray(), WEATHER_HTML_CHARSET);
- is.close();
- baos.close();
- result = "生成成功";
- }catch(Exception e){
- result="写文件过程出错, 取消生成.";
- }
- }else{
- result="获得链接过程出错, 取消生成.";
- }
- }catch(Exception e){
- e.printStackTrace();
- result="获得内容过程出错, 取消生成.";
- }
- return result;
- }
- }
- // 返回字节流
- public String getHtml(JspWriter out) throws Exception {
- //System.setProperty("http.proxyHost", "isaserver");System.setProperty("http.proxyPort", "80");
- java.NET.URL url = new java.NET.URL("http://weather.china.com.cn/city/58362_zx.html");
- java.NET.HttpURLConnection conn = (java.NET.HttpURLConnection) url.openConnection();
- if (conn.getResponseCode() == 200) {
- java.io.InputStream is = (java.io.InputStream) conn.getContent();
- java.io.ByteArrayOutputStream baos =
- new java.io.ByteArrayOutputStream();
- int buffer = 1024;
- byte[] b = new byte[buffer];
- int n = 0;
- while ((n = is.read(b, 0, buffer))> 0) {
- baos.write(b, 0, n);
- }
- String s = new String(baos.toByteArray(), "UTF-8");
- is.close();
- baos.close();
- return s;
- }
- return "";
- }
来源: http://www.bubuko.com/infodetail-2944683.html