日期:2014-05-17 浏览次数:20845 次
??? 今天终于将jsp页面转换为html静态页面这个功能完成!本以为很难的,但是当我完成后发现,这其实不难的,大致的思想是将一个jsp页面读入,然后在写入html文件,具体方法如下:
?
?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @功能描述 将jsp页面转换为HTML文件
* @author Mac.Lee
* @date Aug 31, 2011
* @time 8:06:02 PM
*/
public class CallForHtml {
/*
* 获得jsp页面内容
*/
public static String getHtmlCode(String jsp){
String htmlCode="";
InputStream in=null;
System.out.println(jsp);
try {
URL url=new URL(jsp);
URLConnection connection=url.openConnection();
connection.connect();
in=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in,"utf-8"));
String currentLine;
while((currentLine=reader.readLine())!=null){
htmlCode+=currentLine;
}
reader.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return htmlCode;
}
/**
*
* @ Function 写入html文件
* @param htmlCode html文件内容
* @param path html文件的路径
* @param fileName 文件名
*/
public static synchronized void writeHtml(String htmlCode,String path,String fileName){
PrintWriter pw=null;
if(fileName==""){
/*若生成新闻内容
*則以当前时间为文件名
* */
Date date=new Date();
SimpleDateFormat time=new SimpleDateFormat("yyyyMMddHHmmss");
fileName=time.format(date)+".html";
}
File file=new File(path,fileName);
try {
if(file.exists()){
file.delete();
}else{
file.createNewFile();
}
pw=new PrintWriter(new FileOutputStream(path+"\\"+fileName,true));
pw.println(htmlCode);
pw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件转换成功");
}
}
?
??? 现在,我只能用这个方法,或许有更好的方法
?