Java converts the content in the database to HTML

Source: Internet
Author: User

/**
* Convert the content in the database to HTML
* Data contains fields in the database.
* Formid is the ID of the information in the database.
* Filepath is the location where the generated HTML file is stored
* @ Param Image
* @ Return
*/
Public void formtohtml (string data, string formid, string filepath ){
Stringbuffer Buf = new stringbuffer ("");
Buf. append ("<HTML> ");
Buf
. Append ("<meta http-equiv = 'content-type' content = 'text/html; charset = UTF-8 '/> ");
Buf. append ("<title> sdsds </title> ");
Buf. append ("Buf. append ("Buf. append ("<body> ");
Buf. append (data );
Buf. append ("</body> ");
Buf. append ("String filename = filepath + formid + ". html ";
Objectoutputstream objoutputstream;
Try {
Objoutputstream = new objectoutputstream (New fileoutputstream (
New file (filename )));
Objoutputstream. writeobject (BUF. tostring ());
} Catch (filenotfoundexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}

/**
* Convert the content in the database to HTML
* The location where the vmbasepath VM template is stored, for example, D :\\ projectpath \ attach \ html
* Vmtempname VM Template Name
* Datacontext contains fields in the database.
* The path of HTML generated by htmlfilepath is: "D: \ projectpath \ attach \ HTML \ test.html"
* @ Param Image
* @ Return
*/
Public String datatohtml (string vmbasepath, string vmtempname,
String datacontext, string htmlfilepath) throws exception {
// Initialize and obtain the velocity Engine
Velocityengine VE = new velocityengine ();
// Set the path
Properties Properties = new properties ();
String basepath = vmbasepath;
// String basepath = "d :\\ projectpath \ attach \ html ";
Properties. setproperty (velocity. file_resource_loader_path, basepath );
Ve. INIT (properties );

// Obtain the velocity Template
// Template T = VE. gettemplate ("104.vm ");
Template T = VE. gettemplate (vmtempname );
// Obtain the context of Velocity
Velocitycontext context = new velocitycontext ();

// Enter the data in the context
// Context. Put ("name", "Liang ");
Context. Put ("name", datacontext );

// Output stream
Stringwriter writer = new stringwriter ();

// Conversion output
T. Merge (context, writer );
// Printwriter filewriter = new printwriter (New
// Fileoutputstream ("D: \ projectpath \ attach \ HTML \ test.html"), true );
Fileoutputstream Fos = new fileoutputstream (htmlfilepath );
Printwriter filewriter = new printwriter (FOS, true );
Filewriter. println (writer. tostring ());

Filewriter. Flush ();
Filewriter. Close ();
FOS. Flush ();
FOS. Close ();
System. Out. println ("output completed! ");

Return "success ";
}

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.