DbLookDaiMaBak.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public partial class DbLookDaiMaBak
:
System.Web.UI.Page
{
#region DateTimeGetByStrYyyyMMddHHmmss|yyyyMMddHHmmss转成时间
/// <summary>
/// yyyyMMddHHmmss转成时间
/// </summary>
/// <param name="str">时间格式字符串(要求14位长)|如:20180621020355
/// <returns>如果格式不正确,抛出异常|否则返回时间值|如:2018-06-21 02:03:55</returns>
public DateTime DateTimeGetByStrYyyyMMddHHmmss(string str)
{
if (str == null || str.Length == 0)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "str == null || str.Length == 0"
);
}
int iLen = str.Length;
if (iLen != 14)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "str.Length != 8"
);
}
string dateStr
=
str.Substring(0, 4)
+ "-" + str.Substring(4, 2)
+ "-" + str.Substring(6, 2)
+ " " + str.Substring(8, 2)
+ ":" + str.Substring(10, 2)
+ ":" + str.Substring(12, 2);
return DateTime.Parse(dateStr);
}
/// <summary>
/// yyyyMMddHHmmss转成时间
/// </summary>
/// <param name="str">时间格式字符串(要求14位长)|如:20180621020355
/// <param name="defaultValue">如果时间格式不正确,返回的时间默认值
/// <returns>如果格式不正确,返回默认值defaultValue|否则返回时间值|如:2018-06-21 02:03:55</returns>
public DateTime DateTimeGetByStrYyyyMMddHHmmss(string str, DateTime defaultValue)
{
if (str == null || str.Length == 0)
{
return defaultValue;
}
int iLen = str.Length;
if (iLen != 14)
{
return defaultValue;
}
string dateStr
=
str.Substring(0, 4)
+ "-" + str.Substring(4, 2)
+ "-" + str.Substring(6, 2)
+ " " + str.Substring(8, 2)
+ ":" + str.Substring(10, 2)
+ ":" + str.Substring(12, 2);
DateTime theResult = defaultValue;
if (DateTime.TryParse(dateStr, out theResult))
{
return theResult;
}
return defaultValue;
}
#endregion DateTimeGetByStrYyyyMMddHHmmss|yyyyMMddHHmmss转成时间
#region GetGB2312MD5|获得GB2312(中文编码)格式的MD5值
/// <summary>
/// 获得GB2312编码的MD5值
/// </summary>
/// <param name="s">需要MD5的字符串
/// <returns></returns>
public string GetGB2312MD5(string s)
{
if (s == null)
s = "";
MD5 md5 = new MD5CryptoServiceProvider();
System.Text.Encoding en = System.Text.Encoding.GetEncoding("GB2312");
byte[] t = md5.ComputeHash(en.GetBytes(s));
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < t.Length; i++)
{
sb.Append(t[i].ToString("x").PadLeft(2, ‘0‘));
}
return sb.ToString();
}
#endregion GetGB2312MD5|获得GB2312编码的MD5值
/// <summary>
///
/// </summary>
/// <param name="callName">
/// <param name="dt">
/// <returns></returns>
protected string ToSign(string callName, string dt)
{
string str = callName + dt + "ekinglbs.lookdaima";
return GetGB2312MD5(str);
}
/// <summary>
/// 获得网站根目录(比如网站虚拟目录tools) - http://www.slowx.net/tools/
/// </summary>
/// <returns></returns>
public string GetWebRootUrl()
{
HttpContext hc = HttpContext.Current;
if (hc == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:HttpContext hc = HttpContext.Current值为null。"
);
}
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
// 完整URL地址 //
string strAbsoluteUri = hc.Request.Url.AbsoluteUri;
int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);
if (idx == -1)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
);
}
string theResult = strAbsoluteUri.Substring(0, idx);
if (hc.Request.ApplicationPath == "/")
return theResult;
else
return theResult + hc.Request.ApplicationPath;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
protected string DataBindTheControls(bool isDebug)
{
if (isDebug)
{
string debugWebRootUrl = GetWebRootUrl();
if (debugWebRootUrl.EndsWith("/") || debugWebRootUrl.EndsWith("\\"))
{
debugWebRootUrl = debugWebRootUrl.Substring(0, debugWebRootUrl.Length - 1);
}
return "+" + debugWebRootUrl + "/debugdata.zip";
}
string noPower = "-鉴权失败:" + Request.Url.AbsoluteUri;
string callName = Request.QueryString["CallUserName"];
string dt = Request.QueryString["dt"];
string sign = Request.QueryString["sign"];
if (callName == null || callName.Length == 0)
return noPower + "[callName]";
if (dt == null || dt.Length == 0)
return noPower +"[dt]";
if (sign == null || sign.Length == 0)
{
return noPower + "[sign]";
}
DateTime dtValue = DateTimeGetByStrYyyyMMddHHmmss(dt, DateTime.MinValue);
if (dtValue == DateTime.MinValue)
{
return noPower + "[dt.MinValue]";
}
if (callName != "ekinglbs")
{
return noPower + "[callName=" + callName + "]";
}
string newSign = ToSign(callName, dt);
if (newSign != sign)
{
return noPower + "[callName=" + callName + "]" + "[dt=" + dt + "]" + "[sign=" + sign + "]" + "[newSign=" + newSign + "]";
}
string rootDir = Request.PhysicalApplicationPath + "LookDaiMaDB";
DirectoryInfo dir = new DirectoryInfo(rootDir);
if (!dir.Exists)
{
return "-目录" + dir.FullName + "不存在";
}
FileInfo[] fA = dir.GetFiles();
string webRootUrl = GetWebRootUrl();
if (webRootUrl.EndsWith("/") || webRootUrl.EndsWith("\\"))
{
webRootUrl = webRootUrl.Substring(0, webRootUrl.Length - 1);
}
foreach (FileInfo fi in fA)
{
if (fi == null)
continue;
if (fi.Extension == null)
continue;
if (fi.Extension.Trim().ToLower() != ".trn")
{
continue;
}
if (fi.LastWriteTime.Date != DateTime.Now.Date)
{
continue;
}
return "+" + webRootUrl + "/LookDaiMaDB/" + fi.Name;
}
return "-没有找到文件";
}
/// <summary>
/// 转换成要下载的文件路径
/// </summary>
/// <param name="sender">
/// <param name="e">
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentEncoding
=
System.Text.Encoding.GetEncoding("gb2312");
if (!this.IsPostBack)
{
try
{
string str = DataBindTheControls(false);
Response.Write(str);
}
catch (Exception err)
{
Response.Write("-系统异常:" + err.Message);
}
}
}
}