////// 远程上传从其他网站复制过来的图片 /// public class RemoteUpload { ////// 上传的源文件名称 /// private string sOriginalFileName; ////// 上传后的文件名称 /// private string sSaveFileName; ////// 结合 sContentPath 得取的带路径的上传路径名称 /// private string sPathFileName; ////// 上传需要建立目录的临时变量 /// ///private private string sPath; ////// 根据 sBaseUrl 参数转化后的路径(包含sUploadDir)路径 /// private string sContentPath; ////// 从HTML文本中下载远程图片 /// /// 含有图片网址路径的HTML文本 /// 保存的路径 /// 返回下载的所有图片文件,以“|”间隔! ///返回替换远程图片路径的HTML文本 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; } ////// 保存远程的文件到本地 /// /// 保存到本地文件名 /// 远程读取的文件URL ///如果保存成功则返回 true,否则返回 false ////// 此方法仅供内部使用,由 ReplaceRemoteUrl 进行调用 /// 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; } ////// /// /// ///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; } }