1,寫JAVA代碼
public String patientsindex(HttpServletRequest request){
//獲得當前項目的絕對路徑
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);
//模板檔案
String filePath = path+"\\WEB-INF\\views\\patientsList\\patientsList.jsp";
//圖片路徑
String imagePath ="http://localhost:8080/demo_obj/images/logo1.png";
//建立檔案的路徑
String disrPath = path+"\\WEB-INF\\views\\patientsList\\";
String fileName = "patientsList";
MakeHtml(filePath,imagePath,disrPath,fileName);
return "patientsList/patientsList";
}
/**
* @Title: MakeHtml
* @Description: 建立html
* @param filePath 設定模板檔案
* @param imagePath 需要顯示圖片的路徑
* @param disrPath 產生html的存放路徑
* @param fileName 產生html名字
* @return void 傳回型別
* @throws
*/
public static void MakeHtml(String filePath,String imagePath,String disrPath,String fileName ){
try {
String title = "<image src="+'"'+imagePath+'"'+"/>";
System.err.println("進入:"+filePath);
String templateContent = "";
FileInputStream fileinputstream = new FileInputStream(filePath);// 讀模數板檔案
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;// 產生的html檔案儲存路徑。
FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立檔案輸出資料流
System.out.print("檔案輸出路徑:");
System.out.print(fileame);
byte tag_bytes[] = templateContent.getBytes();
fileoutputstream.write(tag_bytes);
fileoutputstream.close();
} catch (Exception e) {
System.out.print(e.toString());
}
}
2,寫模板檔案位置是JAVA代碼中指定的位置
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理</title>
<style>
body{ text-align:center;border: 0px;margin: 0px;background-color: #F4F4F4;}
.div{ margin:0 auto; width:1188px; height:auto;}
</style>
</head>
<body>
<div class="div">
<div>
###title###
</div>
</div>
</body>
</html>
3.JAVA代碼會重建新的檔案,並替換掉###title###成圖片標籤