Java根據html模板建立 html檔案

來源:互聯網
上載者:User

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###成圖片標籤

 
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.