• url地址从写代码片段


    using System;
    using System.IO;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    using System.IO;

    /// <summary>
            /// 根据虚拟路径获取物理路径
            /// </summary>0
            /// <returns> </returns>
            private void ChangeAspxToHtml()
            {
                // try
                // {
                Boolean boolUrl = false;
                String strRequestUrl = System.Web.HttpContext.Current.Request.RawUrl;//获取请求的原始Url
                String strRedirtUrl = System.Web.HttpContext.Current.Request.Url.ToString();//获取当前请求的Url
                String strPhysicsDirectoryFiles = System.Web.HttpContext.Current.Request.PhysicalPath;//获取当前请求的Url的文件的物理路径
                String strOldRequestFileName = "";
                String strNewRequestFileName = "";
                String strHtmlFileName = "";
                String strRRedirtUrl = "";
                if (CheckUrl(strRequestUrl) == false)
                {
                    Response.Redirect(strRequestUrl + "/default.aspx");
                }
                int intFilePostion = strRequestUrl.LastIndexOf('/');
                strOldRequestFileName = strRequestUrl.Substring(intFilePostion + 1, strRequestUrl.Length - intFilePostion - 1);
                String strPhysicsDirectory = System.IO.Path.GetDirectoryName(Page.Request.PhysicalPath);
                strNewRequestFileName = strOldRequestFileName.Replace(".aspx", "");
                strHtmlFileName = strNewRequestFileName.Replace('?', '_').Replace('&', '_').Replace('=', '_') + ".html";//要转向的html文件

                int intRedirtUrl = strRedirtUrl.LastIndexOf('/');
                strRRedirtUrl = strRedirtUrl.Substring(0, intRedirtUrl) + "/" + strHtmlFileName;
                if (File.Exists(strPhysicsDirectory + "\\" + strHtmlFileName))//判断是存在有该html文件,如果存在,则转向,如果不存在则创建
                {
                    //尝试读取已有文件   
                    Boolean bl = GetFileStream(strPhysicsDirectory + "\\" + strHtmlFileName);
                    //如果文件存在并且读取成功
                    if (bl == true)//没有过期
                    {
                        //using (st)
                        //{
                        // StreamToStream(st, HttpContext.Current.Response.OutputStream);
                        Response.Redirect(strRRedirtUrl);
                        //}
                    }
                    else //写文件
                    {
                        File.Delete(strPhysicsDirectory + "\\" + strHtmlFileName);
                        WriteHtml(strRequestUrl, strPhysicsDirectory + "\\" + strHtmlFileName);
                        Response.Redirect(strRRedirtUrl);
                    }
                }
                else
                {
                    if (WriteHtml(strRequestUrl, strPhysicsDirectory + "\\" + strHtmlFileName) == true)//写文件
                    {
                        Response.Redirect(strRRedirtUrl);
                    }
                }
                // }
                // catch
                // {
                //    Response.Redirect("http://www.xxxx.com");
                // }
            }


            /// <summary>
            /// 生成Html文件
            /// </summary>
            /// <param name="strUrl"> </param>
            /// <param name="strHtml"> </param>
            /// <returns> </returns>
            private bool WriteHtml(String strUrl, String strHtml)
            {

                StringWriter sw = new StringWriter();
                if (strUrl.Contains("?") == false)
                {
                    HttpContext.Current.Server.Execute(strUrl + "?w=1", sw);
                }
                else
                {
                    HttpContext.Current.Server.Execute(strUrl + "&w=1", sw);
                }
                string content = sw.ToString();
                try
                {
                    using (FileStream fs = new FileStream(strHtml, FileMode.Create, FileAccess.Write, FileShare.Write))
                    {
                        using (StreamWriter stw = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
                        {
                            stw.Write(content);
                        }
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }

            /// <summary>
            /// 一小时后过期
            /// </summary>
            /// <param name="filename"> </param>
            /// <returns> </returns>
            private Boolean GetFileStream(string filename)
            {
                try
                {
                    DateTime dt = File.GetLastWriteTime(filename);
                    TimeSpan ts = DateTime.Now - dt;
                    if (ts.TotalHours > 1) //一小时后过期
                    {

                        return false;
                    }
                    else
                    {
                        return true;
                    }
                    //return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                catch
                {
                    return false;
                }
            } 
    /// <summary>
            /// 写文件
            /// </summary>
            /// <param name="src"> </param>
            /// <param name="dst"> </param>
            private void StreamToStream(Stream src, Stream dst)
            {
                byte[] buf = new byte[4096];
                while (true)
                {
                    int c = src.Read(buf, 0, buf.Length);
                    if (c == 0)
                        return;
                    dst.Write(buf, 0, c);
                }
            }

            /// <summary>
            /// 判断Url地址
            /// </summary>
            /// <param name="strUrl"> </param>
            /// <returns> </returns>
            private Boolean CheckUrl(String strUrl)
            {
                if (strUrl.ToLower().EndsWith("com") == true)
                {
                    return false;
                }
                else if (strUrl.ToLower().EndsWith("com/") == true)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
  • 相关阅读:
    并发解决方案
    主外键
    ms
    mq消息丢失
    五种IO模型
    程序运行时间计算gettimeofday&clock_gettime
    exec与xargs区别
    mysql修改数据存放路径(linux)
    od用不同的编码输出文件内容
    segfault at f0 ip 00007f9954108feb sp 00007fffb5e64fc0 error 4 in libxxx.so[7f99540df000+54000]
  • 原文地址:https://www.cnblogs.com/myssh/p/1524034.html
Copyright © 2020-2023  润新知