Java creates HTML files from HTML templates

Source: Internet
Author: User

1, write Java code

Public String Patientsindex (HttpServletRequest request) {

Get the absolute path to the current project
String T=thread.currentthread (). Getcontextclassloader (). GetResource (""). GetPath ();
int Num=t.indexof (". metadata");
String path=t.substring (1,num). Replace ('/', ' \ \ ') +request.getcontextpath (). Replace ("/", "") + "\\WebContent";
SYSTEM.ERR.PRINTLN (path);

Template file
String FilePath = path+ "\\WEB-INF\\views\\patientsList\\patientsList.jsp";
Picture path

String ImagePath = "Http://localhost:8080/demo_obj/images/logo1.png";

Path to new file
String Disrpath = path+ "\\WEB-INF\\views\\patientsList\\";
String fileName = "Patientslist";
Makehtml (Filepath,imagepath,disrpath,filename);
return "Patientslist/patientslist";
}
/**
* @Title: makehtml
* @Description: Creating HTML
* @param filePath Set template file
* @param ImagePath need to show the path of the picture
* @param disrpath to generate HTML storage path
* @param fileName generates HTML name
* @return void return type
* @throws
*/
public static void makehtml (String filepath,string imagepath,string disrpath,string fileName) {
try {
String title = "<image src=" + "' +imagepath+ '" "+"/> ";
System.err.println ("Enter:" +filepath);
String TemplateContent = "";
FileInputStream FileInputStream = new FileInputStream (filePath);//Read template file
int lenght = fileinputstream.available ();
byte bytes[] = new Byte[lenght];
Fileinputstream.read (bytes);
Fileinputstream.close ();
TemplateContent = new String (bytes);
System.err.println ("TemplateContent:" +templatecontent);
TemplateContent = Templatecontent.replaceall ("# # #title ###", title);
System.err.println ("TemplateContent1:" +templatecontent);

String fileame = FileName + ". JSP";
Fileame = Disrpath + fileame;//The generated HTML file save path.
FileOutputStream FileOutputStream = new FileOutputStream (fileame);//create file output stream
System.out.print ("File Output path:");
System.out.print (Fileame);
byte tag_bytes[] = Templatecontent.getbytes ();
Fileoutputstream.write (tag_bytes);
Fileoutputstream.close ();
catch (Exception e) {
System.out.print (E.tostring ());
}
}

2, the Write template file location is the location specified in Java code

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >


<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Management </title>
<style>
body{text-align:center;border:0px;margin:0px;background-color: #F4F4F4;}
. div{margin:0 Auto; width:1188px; height:auto;}
</style>

<body>
<div class= "div" >
<div>
# # #title ###
</div>
</div>
</body>

3.JAVA code will rebuild the new file and replace # # # #title # # #成图片标签


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.