下载服务器图片

    public void Main()
    {
        //图片地址
	DataTable imageList = null;//可文件夹循环,可sql
		
        foreach (DataRow dr in imageList.Rows)
        {
            string imgUrl = dr["hf_fileUrl"].ToString();
            string lourl = "E:\\img\\" + 名称;

            var urls = GetBytesFromUrl(imgUrl);
            if (urls != null)
            {
                WriteBytesToFile(lourl, urls);
            }

        }
    }
    public static byte[] GetBytesFromUrl(string url)
    {
        byte[] b;
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            WebResponse myResp = myReq.GetResponse();

            Stream stream = myResp.GetResponseStream();
            using (BinaryReader br = new BinaryReader(stream))
            {
                b = br.ReadBytes(500000);
                br.Close();
            }
            myResp.Close();
        }
        catch (Exception ex)
        {
            b = null;
        }
        return b;

    }
    public static void WriteBytesToFile(string fileName, byte[] content)
    {
        FileStream fs = new FileStream(fileName, FileMode.Create);
        BinaryWriter w = new BinaryWriter(fs);
        try
        {
            w.Write(content);
        }
        finally
        {
            fs.Close();
            w.Close();
        }

    }
posted @ 2023-07-11 10:57  煜灵  阅读(13)  评论(0)    收藏  举报