我刚开始学做网站,特别想了解如何实现网站优化。个人认为设计的再好的网站都需要优化,但是具体如何把网站优化好呢?
引自探讨高访问量网站优化方案(从图片角度)
可以把网站在所在的图片统一放在一台单独的服务器中,这里本人叫做图片服务器吧.程序中的图片全部链接到图片服务器中.问题是有很多的图片并不是一开始就存在的,大多都是通过后台的添加,修改上传上去的,比如说新闻图片什么的.而asp.net的上传组件只能把图片上传到程序相应的服务器上.这里我们可以利用.net下面的FtpWebRequest类来实现FTP的功能,通过它可能实现类似FTP软件的相关功能,起码有上传和下载,这样就可能轻松的实现图片与程序的完全分离了。
程序如下:
这个方法主要是接收一个客户端文件流通过连接FTP服务器来实现文件的上传.
Code
<summary> /// 利用FTP上传 /// by minjiang 07-10-25 /// </summary> /// <param name="filename">上传的文件名</param> /// <param name="strfileoldname">要保存文件的文件名</param> /// <param name="ftpUserID">ftp用户名</param> /// <param name="ftpPassword">ftp密码</param> private int mFtpUpload(Stream _fileStream, string strfileoldname, string ftpUserID, string ftpPassword) { 初始化信息#region 初始化信息 //上传是否成功 int val = 0; //为文件命名,然后保存 string fileExtension; string ftpServerIP = "192.168.1.1"; //FileInfo fileInf = new FileInfo(filename); //string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; string uri = "ftp://" + ftpServerIP + "/" + strfileoldname; FtpWebRequest reqFTP; // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); //重命名文件的新名称 by minjiang 07-09-05 reqFTP.RenameTo = strfileoldname; // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 指定数据传输类型 reqFTP.UseBinary = true; // 上传文件时通知服务器文件的大小 // reqFTP.ContentLength = fileInf.Length; reqFTP.ContentLength = _fileStream.Length; // 缓冲大小设置为2kb int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; //设置上传进度变量 double dProgess = 1.00; // 打开一个文件流 (System.IO.FileStream) 去读上传的文件 //FileStream fs = fileInf.OpenRead(); FtpWebResponse resFTP = null; #endregion try { // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的2kb //contentLen = fs.Read(buff, 0, buffLength); contentLen = _fileStream.Read(buff, 0, buffLength); // 流内容没有结束 while (contentLen != 0) { // 把内容从file stream 写入 upload stream strm.Write(buff, 0, contentLen); //contentLen = fs.Read(buff, 0, buffLength); contentLen = _fileStream.Read(buff, 0, buffLength); } // 关闭两个流 strm.Close(); //fs.Close(); _fileStream.Close(); val = 1; } catch (Exception ex) { string err = ex.Message.ToString(); if (resFTP != null) { string sResFTP = resFTP.StatusCode.ToString(); } val = 0; } finally { _fileStream.Close(); } return val; } |
这个方法实现文件的上传之间的各种判断操作以及最后调用上传方法进行上传:
Code
/** /// 上传图片,将图片保存到指定目(Ftp) /// by minjiang 07-10-25 /// /// 上传图片控件 /// 保存图片文件夹 /// 上传图片大小上限,..k /// public string[] upPictureFtp(ref System.Web.UI.HtmlControls.HtmlInputFile Fileup, string sSavePath, string preFileName, int iMaxSize) { HttpFileCollection files = HttpContext.Current.Request.Files; string strfileoldpath; //上传原图片的保存路径 string strfileoldname;//图片保存后名称 string fileName, fileExtension; string[] arrayImageUrl = new string[4] { "", "", "", "" }; string fileName_Ftp = ""; try { //没有文件上传 if (Fileup.PostedFile.FileName == "") { arrayImageUrl[2] = "errorNoFile"; return arrayImageUrl; } HttpPostedFile postedFile = null; if (files.Count > 0) { postedFile = files[0]; } else { arrayImageUrl[2] = "errorNoFile"; return arrayImageUrl; } Stream _fileStram= (Stream )postedFile.InputStream; //fileName_Ftp = postedFile.FileName; fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName == "") { arrayImageUrl[2] = "errorNoFile"; return arrayImageUrl; } fileExtension = System.IO.Path.GetExtension(fileName); //判断上传文件类型 string filetype = Fileup.PostedFile.ContentType; //判断是否图片 if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif")) { arrayImageUrl[2] = "errorT"; return arrayImageUrl; } //判断是否图片大小超过上限 int MaxFile = 0; MaxFile = Fileup.PostedFile.ContentLength; if (MaxFile > (iMaxSize * 1024)) { arrayImageUrl[2] = "errorb"; return arrayImageUrl; } //为文件命名,然后保存 Random ra = new Random(); strfileoldname = preFileName + ra.Next(100000, 999999).ToString() + fileExtension; //保存图像 int iOK = this.mFtpUpload(_fileStram, strfileoldname, "ip", "pass"); if (iOK == 1) { arrayImageUrl[0] = strfileoldname; } else { arrayImageUrl[2] = "errorFtp"; } return arrayImageUrl; } catch(Exception e) { arrayImageUrl[2] = e.ToString (); return arrayImageUrl; } } |
这个方法是检测上传的路径是否存在,不存在则创建.
Code
/** /// 检查图片的保存路径是否存在 /// /// private string CheckProductPictureML(string sSavePath) { string strml = this.Server.MapPath(sSavePath); if (Directory.Exists(strml) == false) { Directory.CreateDirectory(strml); } return strml; } |
这种FTP上传形式是把客户端的文件原文不动的传了上去,如果用户手上只有数码图片呢?而数码图片默认一般都在2M以上,一般的用户他们都不会对图片进行优化处理,如果是上传原图片的话,这样起码有两个问题:
第一:在上传速度上成问题;
第二:在网页加载的时候也会太慢.所以我们想将原图片进行缩放处理后只上传处理后的小图片而不用上传原始图片.
程序如下:
Code
/** /// 上传图片,生成小图保存,并不保存原图片,并保存到特定的目录中 /// by minjiang 08-6-4 /// /// 上传图片控件 /// 保存图片文件夹 /// 上传图片大小上限,..k /// public string[] upSmallPicture(ref System.Web.UI.HtmlControls.HtmlInputFile Fileup, string sSavePath, string preFileName, int iWidth, int iHeight) { HttpFileCollection files = HttpContext.Current.Request.Files; string strfileoldpath; //上传原图片的保存路径 string strfileoldname;//图片保存后名称 string fileName, fileExtension; string[] arrayImageUrl = new string[4] { "", "", "", "" }; try { //没有文件上传 if (Fileup.PostedFile.FileName == "") return arrayImageUrl; HttpPostedFile postedFile = null; if (files.Count > 0) { postedFile = files[0]; } else { arrayImageUrl[2] = "errorNoFile"; return arrayImageUrl; } Stream _fileStram = (Stream)postedFile.InputStream; fileName = System.IO.Path.GetFileName(postedFile.FileName); if (fileName == "") return arrayImageUrl; fileExtension = System.IO.Path.GetExtension(fileName); //判断上传文件类型 string filetype = Fileup.PostedFile.ContentType; //判断是否图片 if ((filetype.IndexOf("image") == -1) || (fileExtension.Trim().ToLower() != ".jpeg") && (fileExtension.Trim().ToLower() != ".jpg") && (fileExtension.Trim().ToLower() != ".gif")) { arrayImageUrl[2] = "errorT"; return arrayImageUrl; } //检查保存文件的目录是否存在,否则创建 strfileoldpath = CheckProductPictureML(sSavePath); //为文件命名,然后保存 Random ra = new Random(); strfileoldname = preFileName + "_" + ra.Next(100000, 999999).ToString() + fileExtension; strfileoldpath += "//" + strfileoldname; System.Drawing.Image img = System.Drawing.Image.FromStream(_fileStram); //计算缩放后图片的真正尺寸 int iWidthTemp = iWidth; int iHeightTemp = iHeight; double dHeight = (double)iHeight; double dWidth = (double)iWidth; //图片实际的高 double dImgHeight = (double)img.Height; //图片实际的宽 double dImgWidth = (double)img.Width; //如果上传的图片的宽大于设置的宽或者图片的高大于设置的高 if (dImgWidth > dWidth || dImgHeight > dHeight) { if (dImgWidth / dWidth > (dImgHeight) / dHeight) { dHeight = dWidth / dImgWidth ; dHeight = dHeight * dImgHeight ; iHeight = Convert.ToInt32(dHeight); } else { dWidth = dHeight / dImgHeight; dWidth = dWidth * dImgWidth ; iWidth = Convert.ToInt32(dWidth); } if (iWidth > iWidthTemp) { iWidth = iWidthTemp; } if (iHeight > iHeightTemp) { iHeight = iHeightTemp; } } else { iWidth = (int)dImgWidth ; iHeight = (int)dImgHeight ; } //当上传的小图片的长和高有一个为0时,将它设置成1 解决不能上传的问题 if (iWidth < 1) { iWidth = 1; } if (iHeight < 1) { iHeight = 1; } System.Drawing.Bitmap myBitmap = new Bitmap(img ,iWidth ,iHeight ); //注释部分为Ftp上传 //Stream s = null; //myBitmap.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg); //保存原始图像 //调用方法 mFtpUpload(Stream _fileStream, string strfileoldname, string ftpUserID, string ftpPassword) //保存原始图像 switch (fileExtension) { case ".gif": myBitmap.Save(strfileoldpath, System.Drawing.Imaging.ImageFormat.Gif); break; default: myBitmap.Save(strfileoldpath, System.Drawing.Imaging.ImageFormat.Jpeg); break; } //释放资源 this.clearStram(_fileStram); this.clearBitmap(myBitmap ); this.clearImg(img); return arrayImageUrl; } catch { arrayImageUrl[2] = "error"; return arrayImageUrl; } } /** /// 释放文件流 /// /// 文件流 private void clearStram(Stream _fileStram) { if (_fileStram != null) { _fileStram.Close(); _fileStram.Dispose(); _fileStram.Flush(); } } /** /// 释放位图 /// /// 位图 private void clearBitmap(Bitmap _bitmap) { if (_bitmap != null) { _bitmap.Dispose(); } } /** /// 释放图对象 /// /// 图 private void clearImg(System.Drawing.Image img) { if (img != null) { img.Dispose(); } } |
主要的原理其实特别简单,就是通过上传组件来得到一个客户端文件流,然后通过这个文件流得到一个Image对象,System.Drawing.Image img = System.Drawing.Image.FromStream(_fileStram);
Bitmap 构造函数 (Image, Int32, Int32) :
从指定的现有图像(缩放到指定大小)初始化 Bitmap 类的新实例。
参数 original
类型:System.Drawing.Image
从中创建新 Bitmap 的 Image。
width
类型:System..::.Int32
新 Bitmap 的宽度(以像素为单位)。
height
类型:System..::.Int32
新 Bitmap 的高度(以像素为单位)。
通过Bitmap类的构造函数,可以等到一个经过缩放后的位图对象,最后通过位图对象来将文件进行保存.本人对Image..::.GetThumbnailImage这个方法进行了实验,发现这种方式生成的小图会出现失真的情况.
注: 以上内容转载自 http://www.pcdog.com