posted @ 2007-04-05 16:29 cowboy 阅读(468) 评论(2) 编辑
为了使用方便,我们可以使用自定义控件,如一个文件上传控件,通过指定Ftp站点的相关信息,在文件上传时,自动上传到Ftp服务器.
而在前台显示时,也可以通过一个继承自Image的控件,从一个地址(图片服务器)来取得图片的缩略图.
在图片服务器, 我们可以写一个HttpHandler来实现生成缩略图的过程.
按照这个思路.上传文件时,代码举例如下:
<asp:FtpFileUpload style="display:block;" runat="server" ID="fuPicture" />string GetGifImageUrl()
{
//上传图片
if (this.fuPicture.HasFile)
{
//返回上传到Ftp服务器后的文件名
return this.fuPicture.Save();
}
return "";
}
web.config配置
<FtpFileUploadConfig Server="192.168.2.2" Port="21" UserName="scimg" Password="scimg@sina163.com" HomePath="" AllowExt=".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp"/>
在显示图片时,代码举例如下:
<asp:RemoteImage runat="server" ID="imagePicture" ImageUrl='<%# OperData.Picture %>' />
web.config配置如下:
<RemoteImageConfig RemoteHomeUrl="http://img.xxxxxx.cn/getthumb.aspx" EnableThumb="true"/>
我们已经看到了使用,下面我们来实现它:
首先是FtpFileUpload控件的实现方法
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace Iyond.Web.UI.WebControls
{
/**//// <summary>
/// <section name="FtpFileUploadConfig" type="System.Configuration.SingleTagSectionHandler"/>
/// <FtpFileUploadConfig Server="192.168.2.192" Port="21" UserName="happyfen_images" Password="happyfen_images" HomePath="" >
/// </summary>
public class FtpFileUpload : FileUpload
{
protected FtpFileUploadConfig config = new FtpFileUploadConfig();
private static Regex regexName = new Regex(@"[^\s]*$", RegexOptions.Compiled);
/**//// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="parentPath">父目录</param>
/// <param name="name">文件名</param>
/// <returns></returns>
protected bool CheckFileOrPath(string parentPath, string fileName)
{
//检查一下日期目录是否存在
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(parentPath));
req.Credentials = config.Credentials;
req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
Stream stream = req.GetResponse().GetResponseStream();
using (StreamReader sr = new StreamReader(stream))
{
string line = sr.ReadLine();
while (!string.IsNullOrEmpty(line))
{
GroupCollection gc = regexName.Match(line).Groups;
if (gc.Count != 1)
{
throw new ApplicationException("FTP 返回的字串格式不正确");
}
string path = gc[0].Value;
if (path == fileName)
{
return true;
}
line = sr.ReadLine();
}
}
return false;
}
protected void CreatePath(string parentPath, string name)
{
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(string.Format("{0}/{1}",parentPath,name)));
req.Credentials = config.Credentials;
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.GetResponse();
}
/**//// <summary>
/// 在Ftp服务器上保存文件,并返回文件名
/// </summary>
/// <returns>保存的文件名以及路径</returns>
/// <remarks>
/// 必须在 app.config 中配置
/// </remarks>
public string Save()
{
if (!this.HasFile)
return string.Empty;
if (config.AllowExt.IndexOf(Path.GetExtension(this.FileName)) == -1)
throw new ApplicationException("不允许的文件类型" + Path.GetExtension(this.FileName));
//检查一下日期目录是否存在
string dayPath = DateTime.Today.ToString("yyyyMMdd");
if (!this.CheckFileOrPath("", dayPath))
{
this.CreatePath("", dayPath);
}
string fileName = string.Format("{0}_{1}{2}",Path.GetFileNameWithoutExtension(this.FileName),
DateTime.Now.TimeOfDay.TotalMilliseconds,
Path.GetExtension(this.FileName));
string filePath = string.Format("{0}/{1}",
dayPath, fileName
);
FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(config.GetFtpUri(filePath));
req.Credentials = config.Credentials;
req.Method = WebRequestMethods.Ftp.UploadFile;
Stream upstream = req.GetRequestStream();
for (int byteData = this.FileContent.ReadByte(); byteData != -1; byteData = this.FileContent.ReadByte())
{
upstream.WriteByte((byte)byteData);
}
upstream.Close();
req.GetResponse();
return filePath;
}
}
public class FtpFileUploadConfig
{
private IDictionary config = ConfigurationManager.GetSection("FtpFileUploadConfig") as IDictionary;
/**//// <summary>
/// FTP服务器IP
/// </summary>
public string Server
{
get
{
return config["Server"].ToString();
}
}
/**//// <summary>
/// FTP服务器端口
/// </summary>
public string Port
{
get
{
return config["Port"].ToString();
}
}
/**//// <summary>
/// FTP服务器登陆用户名
/// </summary>
public string UserName
{
get
{
return config["UserName"].ToString();
}
}
/**//// <summary>
/// Ftp服务器登陆密码
/// </summary>
public string Password
{
get
{
return config["Password"].ToString();
}
}
/**//// <summary>
/// 上传的主目录,每个上传的文件建立日期(例:20070203)的目录
/// </summary>
public string HomePath
{
get
{
return config["HomePath"].ToString();
}
}
/**//// <summary>
/// AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp"
/// </summary>
public string AllowExt
{
get
{
return config["AllowExt"].ToString();
}
}
/**//// <summary>
/// 依配置,生成FTP的URI
/// </summary>
/// <param name="relationFilePath"></param>
/// <returns></returns>
public Uri GetFtpUri(string relationFilePath)
{
string uriString = string.Empty;
if (HomePath != "")
{
uriString = string.Format("ftp://{0}:{1}/%2f{2}/{3}", Server, Port, HomePath, relationFilePath);
}
else
{
uriString = string.Format("ftp://{0}:{1}/%2f{2}", Server, Port, relationFilePath);
}
Uri uri = new Uri(uriString);
return uri;
}
/**//// <summary>
/// 依配置,返回ICredentials的实例
/// </summary>
public ICredentials Credentials
{
get
{
return new NetworkCredential(UserName, Password);
}
}
}
}
然后是RemoteImage控件的实现
using System;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.UI.WebControls;
namespace Iyond.Web.UI.WebControls
{
public class RemoteImageHandler : IHttpHandler
{
protected HttpContext context = null;
static Hashtable htmimes = new Hashtable();
internal readonly string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp";
IHttpHandler Members#region IHttpHandler Members
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
this.context = context;
htmimes[".jpeg"] = "image/jpeg";
htmimes[".jpg"] = "image/jpeg";
htmimes[".png"] = "image/png";
htmimes[".tif"] = "image/tiff";
htmimes[".tiff"] = "image/tiff";
htmimes[".bmp"] = "image/bmp";
try
{
string image = context.Request.QueryString["img"];
int width = Convert.ToInt32(context.Request.QueryString["w"]);
int height = Convert.ToInt32(context.Request.QueryString["h"]);
string imagePath = context.Request.MapPath(image);
if (!File.Exists(imagePath))
{
context.Response.End();
return;
}
else if (width == 0 && height == 0)
{
context.Response.Redirect(image);
}
else
{
string imageUrl = GetPicPathUrl(image, width, height);
context.Response.Redirect(imageUrl);
}
}
catch
{
context.Response.End();
}
}
#endregion
Helper#region Helper
/**//// <summary>
/// 获取图像编码解码器的所有相关信息
/// </summary>
/// <param name="mimeType">包含编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串</param>
/// <returns>返回图像编码解码器的所有相关信息</returns>
static ImageCodecInfo GetCodecInfo(string mimeType)
{
ImageCodecInfo[] CodecInfo = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo ici in CodecInfo)
{
if (ici.MimeType == mimeType) return ici;
}
return null;
}
/**//// <summary>
/// 检测扩展名的有效性
/// </summary>
/// <param name="sExt">文件名扩展名</param>
/// <returns>如果扩展名有效,返回true,否则返回false.</returns>
bool CheckValidExt(string sExt)
{
bool flag = false;
string[] aExt = AllowExt.Split('|');
foreach (string filetype in aExt)
{
if (filetype.ToLower() == sExt)
{
flag = true;
break;
}
}
return flag;
}
/**//// <summary>
/// 保存图片
/// </summary>
/// <param name="image">Image 对象</param>
/// <param name="savePath">保存路径</param>
/// <param name="ici">指定格式的编解码参数</param>
void SaveImage(System.Drawing.Image image, string savePath, ImageCodecInfo ici)
{
string path = new FileInfo(savePath).DirectoryName;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//设置 原图片 对象的 EncoderParameters 对象
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)90));
image.Save(savePath, ici, parameters);
parameters.Dispose();
}
#endregion
Methods#region Methods
/**//// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourceImagePath">原图片路径(相对路径)</param>
/// <param name="thumbnailImagePath">生成的缩略图路径,如果为空则保存为原图片路径(相对路径)</param>
/// <param name="thumbnailImageWidth">缩略图的宽度(高度与按源图片比例自动生成)</param>
public void ToThumbnailImages(string sourceImagePath, string thumbnailImagePath, int nWidth, int nHeight)
{
string sExt = sourceImagePath.Substring(sourceImagePath.LastIndexOf(".")).ToLower();
if (sourceImagePath.ToString() == System.String.Empty) throw new NullReferenceException("sourceImagePath is null!");
if (!CheckValidExt(sExt))
{
throw new ArgumentException("原图片文件格式不正确,支持的格式有[ " + AllowExt + " ]", "sourceImagePath");
}
//从 原图片 创建 Image 对象
System.Drawing.Image image = System.Drawing.Image.FromFile(sourceImagePath);
//int num = ((thumbnailImageWidth / 4) * 3);
int width = image.Width;
int height = image.Height;
if (nWidth == 0)
{
nWidth = width;
}
if (nHeight == 0)
{
nHeight = height;
}
//计算图片的比例
double nblh = image.Width * 1.0 / nWidth;
double nblv = image.Height * 1.0 / nHeight;
double nBL = Math.Max(nblh, nblv);// nblh > nblv ? nblh:nblv;
int thumbWidth, thumbHeight;
if (nBL > 1.0)
{
thumbWidth = (int)(image.Width / nBL);
thumbHeight = (int)(image.Height / nBL);
}
else
{
thumbWidth = nWidth;
thumbHeight = nHeight;
}
//用指定的大小和格式初始化 Bitmap 类的新实例
Bitmap bitmap = new Bitmap(thumbWidth, thumbHeight, PixelFormat.Format32bppArgb);
//从指定的 Image 对象创建新 Graphics 对象
Graphics graphics = Graphics.FromImage(bitmap);
//清除整个绘图面并以透明背景色填充
graphics.Clear(Color.Transparent);
//在指定位置并且按指定大小绘制 原图片 对象
graphics.DrawImage(image, new Rectangle(0, 0, thumbWidth, thumbHeight));
image.Dispose();
try
{
//将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
string savepath = (thumbnailImagePath == null ? sourceImagePath : thumbnailImagePath);
SaveImage(bitmap, savepath, GetCodecInfo((string)htmimes[sExt]));
}
catch (System.Exception e)
{
throw e;
}
finally
{
bitmap.Dispose();
graphics.Dispose();
}
}
#endregion
public string GetPicPathUrl(string image, int nWidth, int nHeight)
{
const string pathEnd = "_";
if (nWidth == 0 || nHeight == 0)
{
return image;
}
string imageSmallUrl = string.Format("{0}{1}/{2}_{3}{4}", image, pathEnd, nWidth, nHeight, Path.GetExtension(image));
string imagePath = context.Request.MapPath(image);
string imageSmallPath = context.Request.MapPath(imageSmallUrl);
if (!File.Exists(imageSmallPath))
{
this.ToThumbnailImages(imagePath, imageSmallPath, nWidth, nHeight);
}
return imageSmallUrl;
}
}
public class RemoteImage : System.Web.UI.WebControls.Image
{
protected RemoteImageConfig config = new RemoteImageConfig();
protected Unit width = Unit.Empty;
protected Unit height = Unit.Empty;
public override string ImageUrl
{
get
{
if (this.DesignMode)
{
return base.ImageUrl;
}
else if (config.EnableThumb &&
this.Width.Type == UnitType.Pixel && this.Height.Type == UnitType.Pixel)
{
return string.Format("{0}?img={1}&w={2}&h={3}", config.RemoteHomeUrl, System.Web.HttpUtility.UrlEncode(base.ImageUrl),
this.Width.IsEmpty ? 0 : this.Width.Value, this.Height.IsEmpty ? 0 : this.Height.Value);
}
else
{
return string.Format("{0}/{1}", config.RemoteHomeUrl, base.ImageUrl);
}
}
set
{
base.ImageUrl = value;
}
}
/**//// <summary>
/// 宽度,最好指定象素单位,Image服务器会生成相应宽度的缩略图
/// </summary>
public override Unit Width
{
get
{
return width;
}
set
{
width = value;
}
}
/**//// <summary>
/// 高度,最好指定象素单位,Image服务器会生成相应高度的缩略图
/// </summary>
public override Unit Height
{
get
{
return height;
}
set
{
height = value;
}
}
}
public class RemoteImageConfig
{
private IDictionary config = ConfigurationManager.GetSection("RemoteImageConfig") as IDictionary;
/**//// <summary>
/// 图片服务器HttpHandler地址
/// </summary>
public string RemoteHomeUrl
{
get
{
return config["RemoteHomeUrl"].ToString().TrimEnd('\\','/');
}
}
//是否启用缩略图
public bool EnableThumb
{
get
{
return Convert.ToBoolean(config["EnableThumb"]);
}
}
}
}
posted @ 2008-01-31 10:49 cowboy 阅读(1136) 评论(1) 编辑
情事侦缉档案之:李嘉欣粗口录音分析 只为吃醋破口大骂刘銮雄?
偶当年的梦中情人,大美女李嘉欣一向被评为香港历史上最美丽的港姐,可是近日在网络内流出的一段录音却把她在我心里的最后一点美丽形象破坏殆尽,原来的在她傍上赫赫有名的香港股市“狙击手”刘銮雄这个大款的时候已经损失掉了95%。
网络上这么形容此段录音:“ 在这段49秒的录音内,酷似李嘉欣的女子破口大骂通话中的另一男子,指责他不该捧洪欣,不该与蔡少芬再联络。用词低俗,频爆粗口,令人错愕。而录音中的男主角则让人不免疑惑,尽管声音进行了处理,但是说话的口吻却象足了富商刘銮雄。”
不过我最奇怪的是,为什么这个电话会被录音?这个就要从甲乙双方来看:
A:甲方刘銮雄。也许因为大刘是个有心人,每个电话都录音,以备生意或者其他的不时之需,他日好做呈堂证供吧!
那他为什么要泄露出来?我觉得大刘还是很喜欢李嘉欣的,应当不会这么做的把(如果是我就不会
)!当然也可能是李嘉欣要嫁入许家的传闻,令到大刘不爽,在此也用上股市的狙击术?让许家感受社会舆论压力而嫌弃嘉欣?还有可能是他手下(或曾经的手下)的管录音的人手头紧,所以出卖了,换点零花钱?【手下泄露这个可能性比较小,我个人觉得】
B:乙方李嘉欣。这个我实在想不出什么理由,嘉欣要保存每一个录音,这个还是很费时费力的。而且就算保存了,她在这个时候泄露出来,对她嫁入许家的计划可是大大的不利。退一步就算我们的嘉欣也是一个有心人,也爱保存每个电话录音。那么在这个时候泄露也不是她自己来泄露的,可能是录音也被人用来江湖救急了【此可能性较小】
当然上述分析也不尽然,还有一个很大的可能性就是某些不良媒体为了吸引眼球而无中生有!对这种行为作为嘉欣曾经的粉丝,偶表示愤慨!
最后还是那句老话:时间可以证明一切!希望嘉欣那5%不要消失
以下为本案卷宗,仅供参考:
吃醋痛骂大刘热捧蔡少芬洪欣,对话内容详见下边文字:
刘:你以为她是我女朋友?是我老婆啊?打工而已
李:打工仔?只是打工仔你用对别人那么好啊?
刘:对谁好啊我?
李:你对谁好?你对ada(蔡少芬)不好吗?
刘:我对ada(蔡少芬)还好?
李:所有人都认准你会开公司,会开戏给她拍。这么多联络的?那不用说话了
刘:是以前,以前……
李:你没想过捧她?那你签她回来把x啊?
![]() |
|
posted @ 2007-04-05 16:29 cowboy 阅读(468) 评论(2) 编辑
这里截图一张留有纪念
posted @ 2007-04-03 21:36 cowboy 阅读(209) 评论(0) 编辑
private DotMSN.Messenger messenger = new Messenger();

if (Universal.ConvertNullToEmpty(Request["MsnAccount"]).Equals("") || Universal.ConvertNullToEmpty(Request["MsnPassword"]).Equals(""))
{
throw new UserException("您没有输入MSN帐户或密码!");
}
messenger = new Messenger(); 

try
{
messenger.Connect(Request["MsnAccount"], Request["MsnPassword"]);
if (!messenger.Connected)
{
throw new UserException("MSN无法连接!");
}
messenger.SynchronizeList();
int count = 0;
while (!messenger.GetListEnumerator(MSNList.ForwardList).MoveNext() && count < 5)
{
//while (!messenger.Connected && count < 10) {
System.Threading.Thread.Sleep(2000);
count++;
}
if (!messenger.GetListEnumerator(MSNList.ForwardList).MoveNext() && count == 5)
{
throw new UserException("MSN无法连接!");
}
messenger.SetStatus(MSNStatus.Online); // 设置上线
System.Threading.Thread.Sleep(1000);
ArrayList GroupList = new ArrayList();
Hashtable Grouptable = new Hashtable();
foreach (object o in messenger.ContactGroups.Keys)
{
ContactGroup contactGroup = messenger.ContactGroups[o] as ContactGroup;
GroupList.Add(contactGroup);
Grouptable.Add("Group"+contactGroup.ID, new ArrayList());
}

foreach (Contact contact in messenger.GetListEnumerator(MSNList.ForwardList))
{
ArrayList contactList = Grouptable["Group"+contact.ContactGroup.ID] as ArrayList;
contactList.Add(contact);
}
StringBuilder ListHtml = new StringBuilder();
ListHtml.Append("<tr height=20 bgcolor=#BECFDC align=\"left\">");
ListHtml.Append("<td width=6% align=center><b>展开</b></td>");
ListHtml.Append("<td width=34%><b>电子邮件地址</b></td>");
ListHtml.Append("<td width=60%><b>称呼</b></td></tr>");
bool flag = false;
for (int i=0; i<GroupList.Count; i++)
{
ContactGroup contactGroup = GroupList[i] as ContactGroup;
ArrayList contactList = Grouptable["Group"+contactGroup.ID] as ArrayList;
ListHtml.Append("<tr>");
ListHtml.Append("<td align=center><img src=\"../images/plus.gif\" title=\"合并\" style=\"cursor:hand\" onClick=\"ClickImg(this,'Group"+contactGroup.ID+"')\"></td>");
ListHtml.Append("<td colspan=2 bgcolor=#BEBEDE><input type=\"checkbox\" name=\"Group\" value=\"group"+contactGroup.ID+"\" onClick=\"ClickGroup(this)\" checked>"+(contactGroup.Name.Equals("Individuals")?"未分组":contactGroup.Name)+"("+contactList.Count+")</td>");
ListHtml.Append("</tr>");
ListHtml.Append("<tr id=\"Group"+contactGroup.ID+"\" style=\"display:none\"><td></td><td colspan=2>");
// 得到分组中的好友列表
ListHtml.Append("<table width=100%>");
for (int j=0; j<contactList.Count; j++)
{
Contact contact = contactList[j] as Contact;
ListHtml.Append("<tr bgcolor="+(flag?"#E6E6E5":"#FFFFFF")+">");
ListHtml.Append("<td width=36%><input type=\"checkbox\" name=\"FriendEmail\" value=\""+contact.Mail+"\" group=\"group"+contactGroup.ID+"\" onClick=\"ClickContact(this)\" checked>"+contact.Mail+"</td>");
ListHtml.Append("<td width=64%>"+contact.Name+"</td>");
ListHtml.Append("</tr>");
flag = !flag;
}
ListHtml.Append("</table></td></tr>");
}
lbFriendList.Text = ListHtml.ToString();
}
catch (MSNException ex)
{
throw new UserException("连接失败:"+ex.Message);
}

posted @ 2007-01-24 12:49 cowboy 阅读(994) 评论(1) 编辑
posted @ 2006-11-21 11:30 cowboy 阅读(120) 评论(0) 编辑


