通用类 RemoteUpload 远程上传从其他网站复制过来的图片

/// <summary>
    /// 远程上传从其他网站复制过来的图片
    /// </summary>
    public class RemoteUpload
    {
        
        /// <summary>
        /// 上传的源文件名称
        /// </summary>
        private string sOriginalFileName;
        /// <summary>
        /// 上传后的文件名称
        /// </summary>
        private string sSaveFileName;
        /// <summary>
        /// 结合 sContentPath 得取的带路径的上传路径名称
        /// </summary>
        private string sPathFileName;
        /// <summary>
        /// 上传需要建立目录的临时变量
        /// </summary>
        /// <permission cref="System.Security.PermissionSet">private</permission>
        private string sPath;
        /// <summary>
        /// 根据 sBaseUrl 参数转化后的路径(包含sUploadDir)路径
        /// </summary>
        private string sContentPath;


        /// <summary>
        /// 从HTML文本中下载远程图片
        /// </summary>
        /// <param name="sHTML">含有图片网址路径的HTML文本</param>
        /// <param name="path">保存的路径</param>
        /// <param name="Images">返回下载的所有图片文件,以“|”间隔!</param>
        /// <returns>返回替换远程图片路径的HTML文本</returns>
        public string RtnContent(string sHTML, string path,ref string Images )
        {
            string s_Content = sHTML;
            //设置为在上传内容中搜索指定值的正则表达式
            System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(sHTML, @"((http|https|ftp|rtsp|mms):(\/\/|\\\\){1}(([A-Za-z0-9_-])+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})(\S*\/)((\S)+[.]{1}(gif|jpg|jpeg|bmp|png)))", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            string[] a_RemoteUrl = new string[mc.Count];

            //转入无重复数据
            for(int i = 0;i < mc.Count;i++)
            {
                a_RemoteUrl[i] = (mc[i].Groups[0].ToString());
            }

            //保存的上传后的文件名称
            string SaveFileName;
            //文件类型
            string SaveFileType;

            //上传文件自动建目录标志(年月日)	

            sPath = this.CreateDateTimeDirectory("3");

            sContentPath = path + "upload/";

            string VisualPath = path + "upload/";



            sPath = (sPath.Trim().Equals(System.String.Empty) ? System.String.Empty : System.String.Concat(sPath, "/"));


            //如果不存在建立目录
            if(!Directory.Exists(System.IO.Path.Combine(sContentPath, sPath)))
            {
                Directory.CreateDirectory(System.IO.Path.Combine(sContentPath, sPath));
            }


            sOriginalFileName = sOriginalFileName + "|";
            sSaveFileName = sSaveFileName + "|";
            sPathFileName = sPathFileName + "|";

             //遍历上传内容中允许的文件
            foreach(string str in a_RemoteUrl)
            {
                //获取上传文件的扩展名
                SaveFileType = str.Substring(str.LastIndexOf(".") + 1).ToLower();
                //获取上传后的随机文件名
                SaveFileName = GetRndFileName(SaveFileType);
                //保存远程的文件到本地
                if(SaveRemoteFile(SaveFileName, str))
                {
                    sOriginalFileName += str.Substring(str.LastIndexOf("/") + 1) + "|";
                    sSaveFileName += SaveFileName + "|";
                    sPathFileName += VisualPath + sPath + SaveFileName + "|";
                    s_Content = s_Content.Replace(str, VisualPath + sPath + SaveFileName);                    
                }
            }
            sOriginalFileName = sOriginalFileName.Substring(0, sOriginalFileName.Length - 1);
            sSaveFileName = sSaveFileName.Substring(0, sSaveFileName.Length - 1);
            sPathFileName = sPathFileName.Substring(0, sPathFileName.Length - 1);
            Images = sPathFileName;
            return s_Content;
        }

        private string GetRndFileName(string filetype)
        {
            string filename = StringHelper.RadomFileName();
            return filename + "." + filetype;
        }

        /// <summary>
        /// 保存远程的文件到本地
        /// </summary>
        /// <param name="s_LocalFileName">保存到本地文件名</param>
        /// <param name="s_RemoteFileUrl">远程读取的文件URL</param>
        /// <returns>如果保存成功则返回 true,否则返回 false</returns>
        /// <remarks>
        /// 此方法仅供内部使用,由 ReplaceRemoteUrl 进行调用
        /// </remarks>
        private bool SaveRemoteFile(string s_LocalFileName, string s_RemoteFileUrl)
        {
            bool result = false;
            string AllowExt = ".jpe|.jpeg|.jpg|.png|.tif|.tiff|.bmp|.gif";
            //获取远程文件类型
            string sFileExt;
            sFileExt = s_RemoteFileUrl.Substring(s_RemoteFileUrl.LastIndexOf("."));
            if(AllowExt.IndexOf(sFileExt.ToLower()) == -1)
            {
                result = false;
            }
            else
            {

                using (System.Net.WebClient wc = new System.Net.WebClient())
                {
                    //下载远程文件资源        
                    byte[] buff;
                    try
                    {
                        buff = wc.DownloadData(s_RemoteFileUrl);
                    }
                    catch
                    {
                        return false;
                    }
                    if (buff.Length == 0)
                        return false;
                    if (buff.Length / 1024 > 512000)
                        return false;
                    using (System.IO.FileStream fs = new System.IO.FileStream(sContentPath + sPath + s_LocalFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write))
                    {
                        fs.Write(buff, 0, buff.Length);
                        fs.Close();
                    }
                }

                //加上水印图片
                //try
                //{
                //    WebImages mark = new WebImages();
                //    FileStream fs = new FileStream(sContentPath + sPath + s_LocalFileName, FileMode.Open,FileAccess.Read);
                //    if(!mark.MakeWaterImage(fs, sContentPath + sPath + s_LocalFileName))
                //    {
                //        System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
                //        img.Save(sContentPath + sPath + s_LocalFileName);
                //    }
                //    fs.Close();
                //    fs.Dispose();
                  
                //    mark = null;
                //}
                //catch
                //{

                //}
                
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sign"></param>
        /// <returns></returns>
        private string CreateDateTimeDirectory(System.String sign)
        {
            string path = System.String.Empty;
            if(sign.Equals("0"))
            {
                //不建立目录
            }
            else if(sign.Equals("1"))
            {
                //以年为标志建立目录
                path = DateTime.Now.Year.ToString();
            }
            else if(sign.Equals("2"))
            {
                //以年-月为标志建立目录
                path = System.String.Concat(DateTime.Now.Year.ToString(), "-", DateTime.Now.Month.ToString("00"));
            }
            else if(sign.Equals("3"))
            {
                //以年-月-日为标志建立目录
                path = System.String.Concat(DateTime.Now.Year.ToString(), "-", DateTime.Now.Month.ToString("00"), "-", DateTime.Now.Day.ToString("00"));
            }
            return path;
        }

    }

 

posted @ 2012-08-29 16:03  暗尘掩月  阅读(253)  评论(0编辑  收藏  举报