zoukankan      html  css  js  c++  java
  • 从数据库中下载文件的工具

    Obatain images

     public class Program
        {
            public static void Main(string[] args)
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(@"E:ProjectObtainImagesObtainImagesImageCategloryId.xml");
                //获取节点列表 
                //XmlNodeList topM = xmldoc.SelectNodes("//first");
                XmlNodeList root = xmldoc.SelectNodes("/root");
                XmlNodeList firstNodeList = root[0].ChildNodes;
                //int num = root.Count;
                foreach (XmlNode firNode in firstNodeList)
                {
                    //string address = "D:/data";
                    ReadNode(firNode);
                }
            }
    
            private static void ReadNode(XmlNode node, string saveAddress="D:/data")
            {
                string currentId = node.Attributes["id"].Value;
                string currentAddress = string.Format("{0}/{1}", saveAddress, currentId);
                 List<MatQueue> matQueues = GetImageDataList(currentId);
                if (matQueues.Count() != 0)
                {
                    DownloadImageByAddress(currentAddress, matQueues);
                }
                if (node.HasChildNodes)
                {
                    XmlNodeList nextNodeList = node.ChildNodes;
                    foreach (XmlNode nextNode in nextNodeList)
                    {
                        ReadNode(nextNode,currentAddress);
                    }
                }
            }
            
    
            private static void DownloadImageByAddress(string saveAddress, List<MatQueue> matQueues)
            {
                // 下载图片
                string root = "//172.18.4.2/assets2/";
                string uri_root = "http://local.image.test/imagedir";
                foreach (MatQueue item in matQueues)
                {
                    string dataPath = item.imagepath;
                    string address = root + item.imagepath;
                    string dir = Path.GetDirectoryName(address);
    
                    if (dataPath.Count()<20)
                    {
                        return;
                    }
                    string end_uri = dataPath.Remove(0, 7);
                    string uri = uri_root + end_uri;
    
                    try
                    {
                        if (Directory.Exists(dir))
                        {
                            System.Net.WebClient webClient = new System.Net.WebClient();
                            if (!Directory.Exists(saveAddress))
                            {
                                Directory.CreateDirectory(saveAddress);
                            }
                            webClient.DownloadFile(uri, saveAddress +"/"+ item.materialid + ".jpg");
                            Console.WriteLine(uri);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
    
            private static List<MatQueue> GetImageDataList(string categoryId)
            {
                string address = null;
                List<MatQueue> matQueue = new List<MatQueue>();
    
                using (OracleConnection cn = GetOraConnection())
                {
                    //string sqlGetAddress = "Select caddataaddress From Cadqueue Where extdata = :MaterialId";
                    string sqlGetAddress = "select  materialid, imagepath from pmc.designmaterial q where  q.CATEGORYID = :CategoryId  AND q.imagepath is not null";
                    OracleCommand cmd = new OracleCommand(sqlGetAddress, cn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add(new OracleParameter("CategoryId", categoryId));
                    cn.Open();
    
                    OracleDataReader dtr = cmd.ExecuteReader();
                    while (dtr.Read())
                    {
                        matQueue.Add(PopulateQueuesFromIDataReader(dtr));
                    }
    
                    dtr.Close();
                    cn.Close();
                }
                return matQueue;
            }
    
            public static OracleConnection GetOraConnection()
            {
                string constr = ConfigurationManager.AppSettings["DataBaseString"];
                return new OracleConnection(constr);
            }
    
            public static MatQueue PopulateQueuesFromIDataReader(System.Data.IDataReader dr)
            {
                MatQueue queues = new MatQueue();
    
                //循环读取各字段
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    if (dr[i] != System.DBNull.Value) // 利用属性索引
                    {
                        queues[dr.GetName(i)] = dr[i];
                    }
                }
                return queues;
            }
        }


    属性索引代码

    public class MatQueue
        {
            public string materialid;
            public string imagepath;
    
            //属性索引
            public object this[string propertyName]
            {
                get
                {
                    #region get
                    switch (propertyName.ToLower())
                    {
                        case "materialid":
                            return this.materialid;
                        case "imagepath":
                            return this.imagepath;
                        default:
                            return null;
                    }
                    #endregion
                }
                set
                {
                    #region set
                    switch (propertyName.ToLower())
                    {
                        case "materialid":
                            materialid = Convert.ToString(value);
                            break;
                        case "imagepath":
                            this.imagepath = Convert.ToString(value);
                            break;
                    }
                    #endregion
                }
            }
    
        }

    Xml 结构

    <root>
      <first id="erwqr">
        <second id="fsad">
          <third id="1"></third>
          <third id="2"></third>
          <third id="3"></third>
          <third id="4"></third>
        </second>
      </first>
    </root>
  • 相关阅读:
    mysql之SQL入门与提升(一)
    数据库的主键和外键
    mysql项目实战经验
    浅谈设计模式
    记录下sparkStream的做法(scala)
    hive-hbase-handler方式导入hive表数据到hbase表中
    订单风险系统BI
    关于maven 把插件依赖一起打包进jar问题
    一些hbase的shell查询语句
    关于hive表同步类型问题
  • 原文地址:https://www.cnblogs.com/yaolin1228/p/9089689.html
Copyright © 2011-2022 走看看