Java 项目生成静态页面的代码(2)


    /**
     * generite html source code
     *
     * @author Xing,XiuDong
     * @date 2009.06.22
     * @param request
     * @param url
     * @param toWebRoot
     * @param encoding
     * @throws IOException
     */
    public void genHtml(HttpServletRequest request, String url, boolean toWebRoot, String encoding) throws IOException {

        if (null == url) {
            url = request.getRequestURL().toString();
        }

        String contextPath = request.getContextPath();
        String seq = StringUtils.substring(String.valueOf(new Date().getTime()), -6);

        String ctxPath = super.getServlet().getServletContext().getRealPath(File.separator);
        if (!ctxPath.endsWith(File.separator)) {
            ctxPath += File.separator;
        }

        String filePath = StringUtils.substringAfter(url, contextPath);
        filePath = filePath.replaceAll("\\.(do|jsp|html|shtml)$", ".html");

        String savePath = "";
        String autoCreatedDateDir = "";
        if (!toWebRoot) {
            savePath = StringUtils.join(new String[] { "files", "history", "" }, File.separator);

            String[] folderPatterns = new String[] { "yyyy", "MM", "dd", "" };
            autoCreatedDateDir = DateFormatUtils.format(new Date(), StringUtils.join(folderPatterns, File.separator));

            filePath = StringUtils.substringBefore(filePath, ".html") + "-" + seq + ".html";
        }

        File file = new File(ctxPath + savePath + autoCreatedDateDir + filePath);
        FileUtils.writeStringToFile(file, HTMLGenerator.generate(url), encoding);
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfsfx.html