Java page Static tool class. java

Source: Internet
Author: User

 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

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.