Packagecom.qq.utils;ImportJava.io.File;ImportOrg.apache.commons.httpclient.DefaultHttpMethodRetryHandler;Importorg.apache.commons.httpclient.HttpClient;ImportOrg.apache.commons.httpclient.methods.GetMethod;ImportOrg.apache.commons.httpclient.params.HttpMethodParams;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.io.IOUtils;ImportOrg.apache.log4j.Logger;/*** HTML page static tool *@authorLiujunguang * @date February 22, 2014 morning 11:46:42*/ Public classHtmlgeneratorutils {Private StaticLogger log = Logger.getlogger (htmlgeneratorutils.class); Private Static FinalString encoding = "UTF-8"; /**generate static pages based on templates and parameters*/ Public Static Booleancreatehtmlpage (string url, string htmlfilename) {GetMethod GetMethod=NULL; Try{HttpClient HttpClient=NewHttpClient (); Httpclient.getparams (). Setparameter (Httpmethodparams.http_content_charset, encoding); GetMethod=Newgetmethod (URL); Getmethod.getparams (). Setparameter (Httpmethodparams.retry_handler,NewDefaulthttpmethodretryhandler ());//use the system-provided default recovery policy to automatically retry 3 times when an exception occursGetmethod.addrequestheader ("Content-type", "text/html;charset=utf-8");//sets the character set used when the Get method submits parameters to support normal delivery of Chinese parameters intStatusCode = Httpclient.executemethod (GetMethod);//execute Get method and get return status code, 200 indicates normal, other code is exception if(StatusCode! = 200) {Log.error ("Static page Engine error parsing" + URL + "Generating static page" + Htmlfilename + "!"); } Else{ioutils.copy (Getmethod.getresponsebodyasstream (), Fileutils.openoutputstream (NewFile (htmlfilename)); } return true; } Catch(Exception ex) {Log.error ("The static page engine encountered an error parsing" + URL + "Generating static page" + Htmlfilename + ":" +ex.getmessage (), ex); } finally { if(GetMethod! =NULL) getmethod.releaseconnection (); } return false; } //test Method Public Static voidMain (string[] args) {htmlgeneratorutils.createhtmlpage ("Http://localhost:8080/MSDCP/index.action", "c:/a/a.html"); }}
Java page Static tool class. java