问题描述
我想把动态的jsp页面生成为静态的shtml页面,这样用代码如何做到?最好有简单的例子或者代码提供就最好了。。。。。 问题补充:你这里生成的是html吧,那shtml怎么生成呀??难道原理一样???
解决方案
我写了一个,调试好久,出现中文乱码,不好解决,生成有太多缺点package cn.com;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.RandomAccessFile;import java.net.HttpURLConnection;import java.net.URL;public class Test {public static void main(String[] args) throws Exception {URL url= new URL("//http://127.0.0.1:8080/test/index.jsp"); //你必须启动自己的服务器,如tomcat HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Accept-Language" , "zh-cn"); connection.setRequestProperty("Accept-Charset" , "iso-8859-1,*,utf-8"); String filename = url.getFile();if(!filename.equals("")){ filename = filename.replace("/", "").substring(0,filename.replace("/", "").indexOf("."));}else{filename="index";} File file = new File("/html/"+filename+".html"); //存放在工作间本地磁盘根目录如:F://html/index.html if(file.exists()){ file.delete(); }else{ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); RandomAccessFile ra = new RandomAccessFile(file, "rw"); System.setProperty("sun.jnu.encoding", "utf-8"); //设置属性文件 System.setProperty("file encoding", "utf-8"); String strLine = null; while((strLine = br.readLine()) != null){ ra.writeUTF(new String(strLine.getBytes("utf-8"),"gbk")+"tn"); } ra.close(); br.close(); connection.disconnect(); System.out.println("静态页面生成成功!");}}
解决方案二:
我的上面地址多了二//,写错了,测试换地址时,忘记删除