123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace Common
- {
- /// <summary>
- /// 生成Html静态文件
- /// </summary>
- public sealed class GenerateStaticFileHelper : IDisposable
- {
- #region 配置
- /// <summary>
- /// 生成静态页面的绝对路径,带文件名
- /// </summary>
- private readonly string _staticFilePath;
- /// <summary>
- /// 模板文件字符串
- /// </summary>
- private readonly string _template;
- /// <summary>
- /// 页面编码,默认UTF-8
- /// </summary>
- private readonly string _encode;
- /// <summary>
- /// 写入页面数据流
- /// </summary>
- private static StreamWriter _sw = null;
- /// <summary>
- /// 读取页面数据流
- /// </summary>
- private static StreamReader _sr = null;
- #endregion
- static GenerateStaticFileHelper()
- {
- }
- /// <summary>
- /// 构造
- /// </summary>
- /// <param name="templatePath">模板文件绝对路径,带文件名</param>
- /// <param name="staticFilePath">生成静态页面的相对路径,带文件名</param>
- /// <param name="encode">页面编码,默认UTF-8</param>
- public GenerateStaticFileHelper(string templatePath, string staticFilePath, string encode = "UTF-8")
- {
- //生成存放html的文件夹
- Utils.CreateDirectory(Utils.GetFilePathNoExtensionAndFileName(staticFilePath));
- this._staticFilePath = staticFilePath;
- this._encode = encode;
-
- //模板文件内容
- string cacheTemplateString = ReadTemplateFile(templatePath, this._encode);
- this._template = cacheTemplateString;
- }
- /// <summary>
- /// 生成静态网页
- /// </summary>
- /// <param name="source">特征字符串-待填充的数据</param>
- public bool CreateStaticHtmlFile(Dictionary<string, string> source)
- {
- if (string.IsNullOrEmpty(_template)) return false;
-
- //替换完成后页面内容
- string res = ReplaceTemplateTag(source, _template);
- //生成静态页面
- bool isSucc = CreateStaticHtmlFile(_staticFilePath, this._encode, res);
- return isSucc;
- }
- /// <summary>
- /// 读取文件字节流
- /// </summary>
- /// <param name="templatePath">模板相对路径</param>
- /// <param name="encode">页面编码</param>
- /// <returns>template静态模板的html字符串</returns>
- private static string ReadTemplateFile(string templatePath, string encode)
- {
- try
- {
- Encoding code = Encoding.GetEncoding(encode);
- if (Utils.IsExistFile(templatePath))
- {
- //读取
- using (_sr = new StreamReader(templatePath, code))
- {
- return _sr.ReadToEnd();
- }
- }
- }
- catch (Exception ex)
- {
- return "";
- }
- finally
- {
- if (_sr != null) _sr.Close();
- }
- return "";
- }
- /// <summary>
- /// 替换特征字符串
- /// </summary>
- /// <param name="source">特征字符串-待填充的数据</param>
- /// <param name="streamstr">template静态模板的html字符串</param>
- /// <returns></returns>
- private static string ReplaceTemplateTag(Dictionary<string, string> source, string streamstr)
- {
- if (source != null && source.Count > 0)
- {
- streamstr = source.Aggregate(streamstr, (current, data) => current.Replace(data.Key, data.Value));
- }
- return streamstr;
- }
- /// <summary>
- /// 生成静态页面
- /// </summary>
- /// <param name="staticFilePath">生成静态页面的相对路径</param>
- /// <param name="encode">页面编码</param>
- /// <param name="outhtmlstr">输出的html字符串</param>
- /// <returns></returns>
- private static bool CreateStaticHtmlFile(string staticFilePath, string encode, string outhtmlstr)
- {
- try
- {
- Encoding code = Encoding.GetEncoding(encode);
- //如果存在则删除原来的文件
- Utils.DeleteFile(staticFilePath);
- using (_sw = new StreamWriter(staticFilePath, false, code))
- {
- _sw.Write(outhtmlstr);
- _sw.Flush();
- }
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- finally
- {
- if (_sw != null) _sw.Close();
- }
- }
- /// <summary>
- /// 生成静态页面的文件名
- /// </summary>
- /// <param name="filename">文件名</param>
- /// <param name="extension">文件扩展名</param>
- /// <returns></returns>
- private static string CreateNewFilename(string filename = "", string extension = "htm")
- {
- long tick = DateTime.Now.Ticks;
- string time = DateTime.Now.ToString("yyyyMMddHHmmss");
- Random ra = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
- string rnd = ra.Next(1, 999).ToString();
- string fileName = string.Format("{0}-{1}-{2}-{3}", filename, time, rnd, extension);
- return fileName;
- }
- /// <summary>
- /// 释放资源
- /// </summary>
- public void Dispose()
- {
- _sw.Dispose();
- _sr.Dispose();
- }
- }
- }
|