jsp產生html---pnews
來源:互聯網
上載者:User
js|產生html <%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="java.text.*" %>
<%@ page import="javax.servlet.*,javax.servlet.http.*"%>
<jsp:useBean id="conn" scope="page" class="DBLink.DBSQL"/>
<jsp:useBean id="ReadTemplates" scope="page" class="ball.news.ReadTemplates"/>
<jsp:useBean id="WriteHtml" scope="page" class="ball.news.WriteHtml"/>
<jsp:useBean id="ReplaceAll" scope="page" class="ball.news.ReplaceAll"/>
<%
//request.setCharacterEncoding("gb2312");
try{
String dir=request.getRealPath(".");
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(4194304); //設定檔案大小. 這裡檔案只能上傳4M以內的
fu.setSizeThreshold(4096); //設定緩衝大小.
fu.setRepositoryPath(dir+"/ball/news/images"); //設定臨時目錄.
List fileItems = fu.parseRequest(request); //解析請求,返回一個集合.
Iterator i = fileItems.iterator();
String fieldvalue="";
String ff = "";
String picname="false";
Object tt = "";
Vector v = new Vector();
while(i.hasNext())
{
FileItem fi = (FileItem)i.next();
if(fi.isFormField()) //這是用來確定是否為檔案屬性,
{
String fieldName = fi.getFieldName(); //這裡取得表單名
fieldvalue=fi.getString(); //這裡取得表單值
v.addElement(fieldvalue);
}
else //這裡開始外理檔案
{
File fullFile = new File(fi.getName());
ff = fullFile.getName();
String rr = "";
java.util.Date date2 = new java.util.Date();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); //取得時間
String str2 = formatter.format(date2);
StringTokenizer st = new StringTokenizer(ff,".");
if (st.hasMoreTokens()){
String test12 = st.nextToken();
rr = st.nextToken();
//System.out.println(rr);
}
if (rr.equals("")&&!rr.equals("gif")&&!rr.equals("jpg")&&!rr.equals("jpeg"))
{
picname = "false";
}
else
{
picname = str2+"."+rr; //以時間為圖片名稱
File savedFile = new File(getServletContext().getRealPath("/ball/news/images/"),picname);
fi.write(savedFile); //上傳到伺服器
}
//System.out.println("picname------------------------"+picname);
}
//System.out.println("v------------------------"+v);
}
String[] flag = {"<temp_title>","<temp_date>","<temp_author>","<temp_content>","<str_Temp>","<temp_picture>"};
//將資料寫入到資料庫
Object newtype1 = v.elementAt(0);
String t = newtype1.toString();
int newtype = Integer.parseInt(t);
Object rowid1 = v.elementAt(1);
String rowid = rowid1.toString();
Object title1 = v.elementAt(2);
String title = title1.toString();
Object content1 = v.elementAt(3);
String content = content1.toString();
// System.out.println(down);
java.util.Date date = new java.util.Date();
String strdate = date.toLocaleString();
java.util.Date StrDate1 = new java.util.Date();
String StrDate = StrDate1.toLocaleString(); // 新聞發布時間
//String newtype="0";
conn.openDB();
String sql = "select top 5 * from b_news where newtype="+newtype+" order by id desc";
String strTemp="<tr><td>相關新聞</td></tr>";
ResultSet rs = conn.executeQuery(sql);
while (rs.next())
{
String t2 = rs.getString(2);
String t4 = rs.getString(4);
strTemp += "<tr><td>";
strTemp +="<a href=../../../"+t4+">";
strTemp += t2;
strTemp +="</a>";
}
strTemp +="</td></tr>";
rs.close();
//讀模數板
String filePath = "";
filePath = request.getRealPath("\\ball\\news\\pnews.template");
String templateContent = null;
try{
templateContent = ReadTemplates.getTlpContent(filePath);
System.out.println(templateContent);
}
catch(Exception e)
{
System.out.println("error to template!");
}
//替換模板中的內容
//System.out.println("picname--------------------------------"+picname);
templateContent = ReplaceAll.replace(templateContent,flag[0],title);
templateContent = ReplaceAll.replace(templateContent,flag[1],StrDate);
//templateContent = ReplaceAll.replace(templateContent,flag[2],editer);
templateContent = ReplaceAll.replace(templateContent,flag[3],content);
templateContent = ReplaceAll.replace(templateContent,flag[4],strTemp);
templateContent = ReplaceAll.replace(templateContent,flag[5],picname);
// 根據時間得檔案名稱與路徑名
Calendar calendar = Calendar.getInstance();
String fileName = String.valueOf(calendar.getTimeInMillis()) +".html";
String pathName = request.getRealPath("ball/news")+"\\"+ calendar.get(Calendar.YEAR) + "\\"+ (calendar.get(Calendar.MONTH)+1) +"\\"+ calendar.get(Calendar.DAY_OF_MONTH)+"\\";
String url = calendar.get(Calendar.YEAR) + "/"+ (calendar.get(Calendar.MONTH)+1) +"/"+ calendar.get(Calendar.DAY_OF_MONTH)+"/";
url +=fileName;
//System.out.println(url);
try{
WriteHtml.save(templateContent,pathName,fileName);
}catch(Exception e){
System.out.println("error to html!-----------"+e.getMessage());
}
//寫入資料庫
String sqlInsert = null;
if (!picname.equals("false")){
sqlInsert = "insert into b_news (title,content,url,picture,newtype,addtime,rowid) values ('"+title+"','"+content+"','"+url+"','"+picname+"','"+newtype+"',getdate(),'"+rowid+"')";
}
else
{
picname="images/"+picname;
sqlInsert = "insert into b_news (title,content,url,newtype,addtime,rowid) values ('"+title+"','"+content+"','"+url+"','"+newtype+"',getdate(),'"+rowid+"')";
}
//System.out.println("sql insert---------------"+sqlInsert);
conn.executeUpdate (sqlInsert);
}
catch(Exception e)
{
System.out.println("upload error------------------"+e.getMessage());
}
%>
<%out.println("新聞產生html成功了!");%>