按照我上一次所写的的文章,生成的HTML中会有汉字乱码的问题出现,所以进行了修改,进行另一种方法的实现,还是分为两步:
第一步,先建立一个HTML文件,以此来做一个模板文件,名称为:templateForCallBoard.html,内容如下:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>templateForCallBoard.html</title>
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="this is my page">
- <meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
- </head>
- <body>
- <table width="1000px" align = "center" background="../../img/b156.gif" height = "800px">
- <tr>
- <td width = "100%" align = "center" style = 'color:#ffffff;'>
- <div style = 'border-width:1px;border-style:solid;border-color:#fff394;width:800px;font:normal normal normal medium normal; color:#ffffff;margin-top:15px;margin-bottom:50px;'>
- <div style = " text-align:center;margin-top:15px;line-height:200%;">
- 标题:#title# 发布时间:#time#
- </div>
- <div style = "margin-top:15px;text-align:left;padding-top:8px;border-top:1px solid #fff394;line-height:25px;width:90%;">#content#</div>
- </div>
- </td>
- </tr>
- </table>
- </body>
- </html>
其中:#title# 部分是将来进行替换标题部分,:#time# 是用来替换发布时间的,#content#是用来替换发布内容
第二步,再建立一个SERVLET,名称比如为AddNewAnnounceServlet,post方法如下:
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");//转码,可以接收汉字
- String title = request.getParameter("nameOfAnnounce");//得到发布的文章名称
- String content = request.getParameter("contentOfAnnounce");//得到文章内容
- String realPath = request.getRealPath("/") + "callBoard/templateForCallBoard.html";//模版文件的路径,是按在TOMCAT下的路径
- InputStreamReader in = new InputStreamReader(new FileInputStream(realPath),"UTF-8");//进行文件操作,注意此处的UTF-8是关键,保证了生成的HTML中的汉字不是乱码的
- StringBuffer input = new StringBuffer();
- int ch;
- while((ch = in.read())!=-1){
- input.append((char)ch);
- }
- //以上是进行模版文件的读取
- //以下是进行要发布的文章的名称和内容中的空格和回车等字符转化为HTML可以识别的标签,否则得到的东西将是一行到头
- title = title.replaceAll(" ", " ");
- title = title.replaceAll("/n", "<br>");
- content = content.replaceAll(" ", " ");
- content = content.replaceAll("/n", "<br>");
- //将模版文件内容转化成字符串
- String templateContent = input.toString();
- //得到发布的时间
- //这个类是我自己写的,得到的时间样子是2008-08-29 16:52:13.127主是是为了方便数据库的存储类型DATETIME
- ConverTimeToSqlType ct = new ConverTimeToSqlType();
- String getCurrentTime = ct.converTime();
- //将模版文件得到的字符串中的#title#,#content#,#time#进行替换
- templateContent=templateContent.replaceAll("#time#",getCurrentTime);
- templateContent=templateContent.replaceAll("#title#",title);
- //替换掉模块中相应的地方
- templateContent=templateContent.replaceAll("#content#",content);
- // 根据时间得文件名
- Calendar calendar = Calendar.getInstance();
- String fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
- String filePathUrl = "callBoard/announcement/" + fileame;
- fileame = request.getRealPath("/") + "callBoard/announcement/" + fileame;//生成的html文件保存路径
- OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(fileame),"UTF-8");
- ow.write(templateContent,0,templateContent.length());
- ow.flush();
- ow.close();
- in.close();
- /*
- * 下面要将生成文件和一些消息放到数据库中
- * 下面的是自己写的一些类,是将生成的文章的信息存入数据库,就不贴上来了
- */
- Announcement at = new Announcement();
- at.setAtName(title);
- at.setAtTime(getCurrentTime);
- at.setAtPath(filePathUrl);
- AnnouncementService as = new AnnouncementService();
- HttpSession session = request.getSession();
- int symbol = as.addNewAnnounce( at );
- if( symbol == 0){
- session.setAttribute("errorMsg", "添加公告成功!");
- }else if( symbol == -1 ){
- session.setAttribute("errorMsg", "添加公告失败!");
- }
- response.sendRedirect("../callBoard/error.jsp");
- }
以上的做完了,发布一些基本的文章就可以了,文章的格式和乱码问题基本不会出现了,如果有不清楚的我们可以讨论呵呵。。我的QQ:87928480,希望我们可以共同进步!