也说象新浪、搜狐等大型网站的新闻系统2003-11-14 9:48:53
现在想起来还是用java写起来简单。 主要思想就是做好n个模板然后在模板中设置标志以后替换这些标志就可以了。因为asp不能直接读取文件流所以把他写在数据库中,然后进行处理。 下面说一下在java中的做法,主题思想一样,如果你用过PHP Templates 就非常明白我在说什么了。不明白可以参考本站的文章。 代码非常简单,不必做任何解释: 表单文件 FileName:Form.html [code] [/code] 模板文件: FileName:detail.tpl [code] [b]H_artBody[/b] 作者:[b]H_artAuthor[/b] 时间:[b]H_artDate[/b] 出自:[b]H_artFrom[/b] [/code] Servlet文件: FileName:CreateHtml.java [code]import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.text.*; import TextTools; /** 从指定模板文件中读取数据然后把把设置的变量替换掉同时生成HTML文件 File:CreateHtml.java @Author:Alec Cheung MSN:eboysit@hotmail.com Date:2003-5-21 Home:http://cheung.cxc.cc http://www.andyfans.com http://97741.126.com Oicq:1695925 */ public class CreateHtml extends HttpServlet { TextTools myText; Date date = new Date(); SimpleDateFormat sf,sdf; public void init(ServletConfig conf) throws ServletException{ super.init(conf); this.myText = new TextTools(); this.sf = new SimpleDateFormat("yyyyMMdd"); this.sdf = new SimpleDateFormat("yyyy-MM-dd"); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{ String fileInputPath = "D:\\\resin-2.1.4\\\webapps\\\testServlet\\\templates\\\detail.tpl"; String fileOutPath = "D:\\\resin-2.1.4\\\webapps\\\testServlet\\\html\\\"; String newBody = req.getParameter("artBody"); //"这是内容"; newBody = myText.iso2gb(newBody); newBody = myText.addHTML(newBody); String newTitle = req.getParameter("artTitle"); //"这是个标题"; newTitle = myText.iso2gb(newTitle); String newAuthor = req.getParameter("artAuthor");// "张利海"; newAuthor = myText.iso2gb(newAuthor); String newFrom = req.getParameter("artFrom"); //"新浪网"; newFrom = myText.iso2gb(newFrom); String newDate = sdf.format(date); String artID = "12"; //当前新闻编号一般从数据库中取得当前id String newFileName= sf.format(date)+"_"+artID+".html"; //"2003535.html"; StringBuffer buf = new StringBuffer(); int b; try{ BufferedReader br = new BufferedReader(new FileReader(fileInputPath)); while((b=br.read()) != -1){ buf.append((char) b); }//end while br.close(); PrintWriter pw = new PrintWriter(new FileOutputStream(fileOutPath+newFileName)); pw.println(myText.temp2Html(buf.toString(),newTitle,newBody,newAuthor,newDate,newFrom)); pw.close(); }//end try catch(IOException ex){} }//end doPost() public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { PrintWriter out = res.getWriter(); out.println("This Java Servlet doPost Method!"); } public void destroy(){ } }[/code] 其中用到的工具文件 FileName:TextTools.java [code]import java.util.*; import java.io.*; /** 字符串转换工具 File:TextTools.java @Author:Alec Cheung MSN:eboysit@hotmail.com Date:2003-5-21 Home:http://cheung.cxc.cc http://www.andyfans.com http://97741.126.com Oicq:1695925 */ public class TextTools { public TextTools(){ } public String Replace(String sourceStr,String oldStr,String newStr){ if(sourceStr==null) return null; int sourceLen = sourceStr.length(); int oldLen = oldStr.length(); int starPost = 0; int post = 0; StringBuffer buf = new StringBuffer(); while ((post = sourceStr.indexOf(oldStr,starPost)) >= 0) { buf.append(sourceStr.substring(starPost,post)); buf.append(newStr); starPost = post + oldLen; } if (starPost < sourceLen) { buf.append(sourceStr.substring(starPost)); } return buf.toString(); } public String temp2Html(String sourceStr,String artTitle,String artBody,String artAuthor,String artDate,String artFrom){ sourceStr = Replace(sourceStr,"H_artTitle",artTitle); sourceStr = Replace(sourceStr,"H_artBody",artBody); sourceStr = Replace(sourceStr,"H_artAuthor",artAuthor); sourceStr = Replace(sourceStr,"H_artDate",artDate); sourceStr = Replace(sourceStr,"H_artFrom",artFrom); return sourceStr; } public String addHTML(String sourceStr){ sourceStr = Replace(sourceStr,">",">"); sourceStr = Replace(sourceStr,"<","<"); sourceStr = Replace(sourceStr,"\r\n"," "); sourceStr = Replace(sourceStr,"\"","""); return sourceStr; } public String iso2gb(String str) { if (str != null) { byte[] tmpbyte=null; try { tmpbyte=str.getBytes("ISO-8859-1"); } catch (UnsupportedEncodingException e) { //System.out.println("Error: Method: dbconn.iso2gb :"+e.getMessage()); } try { str=new String(tmpbyte,"GBK"); } catch(UnsupportedEncodingException e) { //System.out.println("Error: Method: dbconn.gb2iso :"+e.getMessage()); } } return str; } }[/code] 只是个简单的例子如果需要图片还需要扩展方法,并且同时把文章标标题和文件名写到数据库中,以便以后检索用。 |
|
|