JSP generates HTML---pnews

Source: Internet
Author: User
Tags date file size getdate getmessage insert tostring
js| generates 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); Sets the file size. This file can only be uploaded within 4M.

Fu.setsizethreshold (4096); Sets the buffer size.

Fu.setrepositorypath (dir+ "/ball/news/images"); Set up a temporary directory.

List Fileitems = fu.parserequest (request); Resolves the request, returning a collection.


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 ())//This is used to determine whether it is a file attribute,
{

String fieldName = Fi.getfieldname (); Here to get the table Single-name
Fieldvalue=fi.getstring (); This gets the form value

V.addelement (Fieldvalue);

}

else//Start the foreign file here
{

File FullFile = new file (Fi.getname ());
FF = Fullfile.getname ();
String rr = "";


Java.util.Date date2 = new Java.util.Date ();
SimpleDateFormat formatter = new SimpleDateFormat ("Yyyymmddhhmmss"); Get time
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; Take time as Picture name

File Savedfile = new file (Getservletcontext (). Getrealpath ("/ball/news/images/"), picname);

Fi.write (Savedfile); Upload to Server
}
System.out.println ("Picname------------------------" +picname);
}
System.out.println ("v------------------------" +v);
}
String[] flag = {"<temp_title>", "<temp_date>", "<temp_author>", "<temp_content>", "<str_ Temp> "," <temp_picture> "};

Writing data to a database
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 (); Press release time
String newtype= "0";
Conn.opendb ();
String sql = "SELECT Top 5 * The b_news where newtype=" +newtype+ "ORDER by id DESC";
String strtemp= "<tr><td> related news </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 ();


Reading templates

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!");
}
Replacing content in a 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);

File name and path name based on time

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 ());
}


Writing to the database
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 ());
}



%>
&LT;%OUT.PRINTLN ("News generation HTML succeeded!") ");%>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.