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 '1',
`special_id` int(11) default '0',
`tuijian` int(11) default '0',
`redian` int(11) default '0',
`newss` int(11) default '0',
`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('產生靜態新聞頁成功');window.location.href='addMusic.jsp'</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工程