• .net下载图片,去下载重复的图片,保存数据库,保存图片


        public static string DowImg(string content1, string key)
        {
            string content = content1;
            MatchCollection m = getregduo("<img.+?>", content);
            foreach (Match item in m)
            {//

                string imgurl = getreg("src=\"(.+?)\"", item.Value);//原图片url地址
                if (imgurl.IndexOf("lkjfds")!=-1)
                {
                    string ddd = "ddd";
                }
      string houzui = imgurl.Substring(imgurl.LastIndexOf("."));//图片的后缀
              
                string imgname = Guid.NewGuid().ToString() + houzui;
                string name = "http://www.lkjfds.com/taoimg/" + key + "/" + imgname;//改后的图片路径;
              
              
           
                try
                {  
                    DAL.Database.ExecProc("insert into img(url,houurl) values('" + imgurl + "','"+name+"')");//把已有的链接放入数据库
                    content = content.Replace(imgurl, name);
                }
                catch (Exception e)
                {
                    if (e.ToString().IndexOf("索引") != -1)//说明这幅图片已经存在于数据库了
                    {
                        name = DAL.Database.ReturnScalar("select houurl from img where url='" + imgurl + "'").ToString();
                        content = content.Replace(imgurl, name);
                        continue;
                    }
                    throw;
                }
               // 如果不是就下载图片
                Stream ss = httpweb.getGet(imgurl, new System.Net.CookieContainer());//下载图片
                Image img = Image.FromStream(ss);
                img.Save(Environment.CurrentDirectory + "/" + key + "/" + imgname);//保存图片
            }
            return content;
        }

  • 相关阅读:
    Maven导入com.google.common.collect jar包
    poj 2192 Zipper
    poj 3278 Catch That Cow
    poj 2488 A Knight's Journey
    poj 3982 序列
    poj 2109 Power of Cryptography
    poj 3258 3273
    java中大数的一些基本运算
    hdu 1715 大菲波数
    最小生成树模板
  • 原文地址:https://www.cnblogs.com/djhama/p/1796221.html
Copyright © 2020-2023  润新知