Required package cobra. Jar Js. Jar
Private Boolean crearejsp (string jspname, String title, Boolean isydyw, string yuanwen, string zhengwen, string topimg, date ){
String savepath = servletactioncontext. getservletcontext (). getrealpath ("/");
String Path = topimg. substring (topimg. indexof ("/", 1 ));
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
String dateformat = SDF. Format (date );
String filepath = servletactioncontext. getservletcontext (). getrealpath ("wxpage") + "/" + jspname;
Outputstreamwriter fw = NULL;
Try {
FW = new outputstreamwriter (New fileoutputstream (filepath), "UTF-8 ");
Stringbuffer sb = new stringbuffer ();
// Sb. append ("<% @ page Language = \" Java \ "contenttype = \" text/html; charset = UTF-8 \ "pageencoding = \" UTF-8 \ "%> \ r \ n ");
SB. append ("<! Doctype HTML public \ "-// W3C // dtd xhtml 1.0 transitional // en \" \ "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\"> \ r \ n ");
SB. append ("<HTML xmlns = \" http://www.w3.org/5o/xhtml\ "> \ r \ n ");
SB. append ("SB. append ("<meta http-equiv = \" Content-Type \ "content = \" text/html; charset = UTF-8 \ "/> \ r \ n ");
SB. append ("<meta name = \" viewport \ "content = \" width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0; \ "/> \ r \ n ");
SB. append ("<meta name = \" format-detection \ "content = \" telephone = NO \ "/> \ r \ n ");
SB. append ("<title>"). append (title). append ("</title> \ r \ n ");
// Sb. append ("<link href = \" CSS/use.css \ "rel = \" stylesheet \ "type = \" text/CSS \ "/> \ r \ n ");
SB. append ("<style type = \" text/CSS \ "> \ r \ n ");
SB. append ("<! -- \ R \ n ");
SB. append ("# biaoti {\ r \ n ");
SB. append ("width: 70%; \ r \ n ");
SB. append ("Max-width: 540px; \ r \ n ");
SB. append ("font-family: \" \ "; \ r \ n ");
SB. append ("font-size: 20px; \ r \ n ");
SB. append ("text-align: Left; \ r \ n ");
SB. append ("padding-top: 5px; \ r \ n ");
SB. append ("} \ r \ n ");
SB. append ("# riqi {\ r \ n ");
SB. append ("width: 70%; \ r \ n ");
SB. append ("Max-width: 540px; \ r \ n ");
SB. append ("font-family: \" \ "; \ r \ n ");
SB. append ("font-size: 12px; \ r \ n ");
SB. append ("text-align: Left; \ r \ n ");
SB. append ("padding-top: 5px; \ r \ n ");
SB. append ("color: # CCC; \ r \ n ");
SB. append ("height: 10px; \ r \ n ");
SB. append ("} \ r \ n ");
SB. append ("A: link {\ r \ n ");
SB. append ("font-size: 13px; \ r \ n ");
SB. append ("text-Decoration: none; \ r \ n ");
SB. append ("font-family: \" \ "; \ r \ n ");
SB. append ("color: #667b99; \ r \ n ");
SB. append ("} \ r \ n ");
SB. append ("A: hover {\ r \ n ");
SB. append ("font-size: 13px; \ r \ n ");
SB. append ("color: #667b99; \ r \ n ");
SB. append ("text-Decoration: underline; \ r \ n ");
SB. append ("} \ r \ n ");
SB. append ("--> \ r \ n ");
SB. append ("</style> \ r \ n ");
SB. append ("SB. append ("<body leftmargin = \" 0 \ "topmargin = \" 0 \ "marginwidth = \" 0 \ "marginheight = \" 0 \ "bgcolor = \" # ffffff \ "Style = \" padding: 10px 10px 2px 10px; \ "> \ r \ n ");
SB. append ("<Div class = \" biaoti \ "id = \" biaoti \ "> "). append (title ). append ("</div> \ r \ n ");
SB. append ("<Div class = \" riqi \ "id = \" riqi \ "> "). append (dateformat ). append ("</div> \ r \ n ");
Bufferedimage sourceimg = ImageIO. Read (New fileinputstream (savepath + path ));
If (sourceimg. getwidth () & gt; 600 ){
SB. append ("<p> </P> ");
} Else {
SB. append ("<p> </P> ");
}
Zhengwen = parsehtml (zhengwen );
SB. append (zhengwen );
If (isydyw ){
SB. append ("<p> <a href = \""). append (yuanwen ). append ("\""). append ("> "). append ("read the original article </a> </P> ");
}
SB. append ("</body> \ r \ n ");
SB. append ("FW. Write (sb. tostring ());
} Catch (exception e ){
Logger. Error ("JSP generation failed", e );
Return false;
} Finally {
Try {
FW. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
// Boolean result = copefile (filepath );
Return true;
}
Private string parsehtml (string htmlcontent) throws saxexception, ioexception {
Reader reader = new stringreader (htmlcontent );
Inputsourceimpl inputsource = new inputsourceimpl (reader,
"");
Useragentcontext uacontext = new simpleuseragentcontext ();
Documentbuilderimpl builder = new documentbuilderimpl (uacontext );
Document d = builder. parse (inputsource );
Html#entimpl document = (html#entimpl) D;
Nodelist NL = Document. getelementsbytagname ("IMG ");
For (INT I = 0; I <NL. getlength (); I ++ ){
Htmlimageelement IMG = (htmlimageelement) nL. item (I );
If (IMG. hasattribute ("width ")){
IMG. removeattribute ("width ");
}
String savepath = servletactioncontext. getservletcontext (). getrealpath ("/");
String Path = IMG. getsrc (). substring (IMG. getsrc (). indexof ("/", 1 ));
Bufferedimage sourceimg = ImageIO. Read (New fileinputstream (savepath + path ));
If (sourceimg. getwidth () & gt; 600 ){
IMG. setattribute ("style", "width: 100%; max-width: 640px ;");
} Else {
IMG. setattribute ("style", "width: 100%; max-width:" + sourceimg. getwidth () + "PX ;");
}
}
Return document. getinnerhtml ();
}