下载URL图片到本地
//下载照片
private static void DownFileByUrl(string url,string chatid,int time)
{
Stream reader = null;
FileStream stream = null;
try
{
WebRequest request = WebRequest.Create(url);
WebResponse response = request.GetResponse();
reader = response.GetResponseStream();
//239.9
stream = File.Open("d: \\website\\userphoto\\" + chatid + "_" + time + ".jpg", FileMode.OpenOrCreate, FileAccess.Write);
//本机
//stream = File.Open("d: \\userphoto\\" + chatid + "_" + time + ".jpg", FileMode.OpenOrCreate, FileAccess.Write);
//stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buf = new byte[512];
int count = 0;
while ((count = reader.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, count);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (reader != null)
{
reader.Close();
stream.Close();
}
}
}