现在的位置: 首页 > 综合 > 正文

jsp生成静态新闻页面(三)

2013年08月28日 ⁄ 综合 ⁄ 共 5555字 ⁄ 字号 评论关闭

1. 修改添加新闻内容 的 用 文件编辑器ewebeditor

2.把文件名,标题存入数据库中,方便前台调用这些生成的静态新闻页面.

 

 

1加ewebeditor后 图片显示出错
 生成的是
<IMG src="/UploadFile/20080716035903984.gif">
但是这个静态新闻页是在2008-07-15这个文件夹里面的,所以要变成
<IMG src="../UploadFile/20080716035903984.gif">才可以显示正确
(注意:这里我把这个工程设置成根目录的)
解决:
 String article =new String(request.getParameter("article").getBytes("iso-8859-1"),"GB2312");
 int index= article.indexOf("IMG");
   System.out.print(article);  //<IMG src="/UploadFile/20080716035903984.gif">
   if(article.indexOf("IMG")>0)
   {
    article=article.replaceAll("/UploadFile","../UploadFile");
   }
     System.out.print(article);//<IMG src="../UploadFile/20080716035903984.gif">

2.插入数据库(生成的html,如2008-07-16/1216200066859.html,歌曲名,和添加时间)
CREATE TABLE `smusic` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(100) default NULL,
  `filesrc` text,
  `singer_id` varchar(11) default NULL,
  `type_id` varchar(11) default &apos;1&apos;,
  `special_id` int(11) default &apos;0&apos;,
  `tuijian` int(11) default &apos;0&apos;,
  `redian` int(11) default &apos;0&apos;,
  `newss` int(11) default &apos;0&apos;,
  `publictime` datetime default NULL,
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;

 <%@ page language="java" contentType="text/html;charset=utf-8" errorPage=""%>
<%@ page import="java.util.*,java.io.*,java.text.*,my.bean.*"%>
<jsp:directive.page import="com.pp.db.DBOperate"/>

 

<%
 String title=new String(request.getParameter("title").getBytes("iso-8859-1"),"GB2312");

 String url =new String(request.getParameter("url").getBytes("iso-8859-1"),"GB2312");
  String lyric =new String(request.getParameter("lyric").getBytes("iso-8859-1"),"GB2312");
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  String nowTime=format.format(new Date());
  String fileame="";
  int a = 0;
  try{
    String filePath = "",path="";
    filePath = request.getRealPath("/")+"modle.html";
    filePath = filePath.replaceAll("////","/");
    String templateContent="";
 
    //读取hmtl模板文件
    FileInputStream fis = new FileInputStream(filePath);
    StringBuffer content = new StringBuffer();
    DataInputStream in = new DataInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    String line = null;
    while ((line = br.readLine()) != null)
    content.append(line + "/n");
    br.close();
    in.close();
    fis.close();
 
    //替换模板里面的相关部分
    templateContent = new String(content);
    templateContent=templateContent.replaceAll("###title###",title);
    templateContent=templateContent.replaceAll("###url###",url);
    templateContent=templateContent.replaceAll("###addtime###",nowTime);
    templateContent=templateContent.replaceAll("###lyric###",lyric);//替换掉模块中相应的地方
   System.out.println(templateContent);
    Calendar calendar = Calendar.getInstance();
    fileame = String.valueOf(calendar.getTimeInMillis()) +".html";
    String wfileame=fileame;
    //建立新闻页面存放目录
    path= request.getRealPath("/")+nowTime+"/";
    File d=new File(path);//建立代表Sub目录的File对象,并得到它的一个引用
    if(!d.exists()){//检查Sub目录是否存在
       d.mkdir();//建立Sub目录
     }
    File f=new File(path,fileame);
    if(!f.exists()){//检查File.txt是否存在
       f.createNewFile();//在当前目录下建立一个名为File.txt的文件
    }
    fileame = request.getRealPath("/")+nowTime+"/"+fileame;//生成的html文件保存路径
   
    //替换后的内容写入到文件
    FileOutputStream fos = new FileOutputStream(fileame);
    Writer output = new OutputStreamWriter(fos, "UTF-8");
    output.write(templateContent);
    output.close();
    fos.close();
   
    //我也要写到数据库中,方便前台调用啦
    DBOperate dbo=new DBOperate();
      Smusic smusic=new Smusic();
      smusic.setName(new String(title.getBytes("GB2312"),"iso-8859-1"));
      smusic.setFilesrc(nowTime+"/"+wfileame);
      smusic.setPublictime(nowTime);
      dbo.insert_update(smusic,"insert");
     
    out.println(" <script>window.alert(&apos;生成静态新闻页成功&apos;);window.location.href=&apos;addMusic.jsp&apos;</script>");
 
  }catch(Exception e){
    System.out.print(e.toString());
  }
%>

3示例
<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312"%>
<%@ page import="com.pp.db.*" %>
<%@ page import="my.bean.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>index</title>
</head>
<script language="javascript" type="text/javascript">

function reverseSelect()
{

    for(var i=0;i<songList.elements.length;i++)
        if(songList.elements[i].checked == true)
            songList.elements[i].checked = false;
        else
            songList.elements[i].checked = true;
}
function AllSelect()
{
    for(var i=0;i<songList.elements.length;i++)
        songList.elements[i].checked=true;
}
</script>
<body>
<h2 align="center"><em><strong><font color="#FF0000">在线音乐播放演示</font></strong></em></h2>
<form name="songList" method="post" action="Process.jsp">
    <div id="Layer1" style="position:absolute; width:783px; height:154px; z-index:1; left: -5px; top: 141px;">
    <table width="780" border="0" cellspacing="0" cellpadding="5">
     <%
      DBOperate dbo=new DBOperate();
     dbo.setLength(10);
      String hql="from  Smusic"; 
      List mylist=dbo.findSQLHibernate(hql,1);
      int n=mylist.size();
      for(int i=0;i<mylist.size();i++)
      {
        Smusic music=(Smusic)mylist.get(i);
  
      %>
       <tr>
        <td width="714"><ul><li><a href="<%=music.getFilesrc()%>"> <%=new String(music.getName().getBytes("iso-8859-1"),"GB2312")%></a></li></ul></td>
        <td width="25"><input type="checkbox" name="name<%=i%>" value="<%=music.getId() %>"></td>
      </tr>
     <%
         }
      %>
    </table>
  </div>
      <input type="button" name="All" value=" 全 选 " onClick="AllSelect()">
      <input type="button" name="Reverse" value=" 反 选 " onClick="reverseSelect()">
      <input type="reset" name="Reset" value=" 重 置 ">
      <input type="submit" name="Submit" value=" 播 放 ">
      <input type="hidden"  name="size" value="<%=n%>">
</form>
</body>
</html>

附件:csdn空间/musichouse2工程

抱歉!评论已关闭.