zoukankan      html  css  js  c++  java
  • .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;
        }

  • 相关阅读:
    php extends
    php 冒泡排序
    php base64_encode和base64_decode 编码/解码url
    php use
    php命名空间示范
    php 传一个url抓取此页面所有的链接
    pyspark
    【P1330】 封锁阳光大学
    [p1967] 货车运输
    分治的思想
  • 原文地址:https://www.cnblogs.com/djhama/p/1796221.html
Copyright © 2011-2022 走看看