asp.net 根据连接地址保存文件,图片

第一种方式  下载图片,文件

WebClient my = new WebClient();

string url = "http://localhost:1015/resource/loading.gif";
string url2 = HttpContext.Current.Request.MapPath("~\\resource\\root\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff")) + url.Substring(url.IndexOf("."), url.Length - url.IndexOf("."));
my.DownloadFile(url, url2 );

第二种方式  下载图片,文件

string path = System.Windows.Forms.Application.StartupPath+"\\1.jpg";//下载到的地址+文件名

// 设置参数
HttpWebRequest request = WebRequest.Create("http://XXXX:8080/resource/root/服务券/生日礼/10月/twg160922_392x120.jpg") as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(path, FileMode.Create);

byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();

上一篇:Visual Studio下,打开资源视图rc文件,提示在另一个编辑器中打开


下一篇:在Java中直接调用js代码(转载)