时间:2021-05-20
本文为大家分享了HttpWebRequest实现下载图片至本地的具体代码,供大家参考,具体内容如下
HttpWebRequest发送web请求,获取流文件,保存至本地
using System;using System.IO;using System.Net;using System.Text;using System.Web.Mvc; namespace Web.Controllers{ public class HomeController : Controller { string url = "http:///Image.aspx?ucode=ucode"; public Stream GetFileToStream(string filePath) { FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] bytes = new byte[fileStream.Length]; fileStream.Read(bytes, 0, bytes.Length); fileStream.Close(); Stream streamFile = new MemoryStream(bytes); return streamFile; } public void Method1() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.ServicePoint.Expect100Continue = false; req.Method = "GET"; req.KeepAlive = true; req.ContentType = "image/png"; HttpWebResponse rsp = (HttpWebResponse)req.GetResponse(); System.IO.Stream stream = null; try { //以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream(); System.Drawing.Image.FromStream(stream).Save(pathName); } finally { // 释放资源 if (stream != null) stream.Close(); if (rsp != null) rsp.Close(); } } protected void Method2() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; HttpWebResponse myResponse = null; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); Stream stream = myResponse.GetResponseStream(); #region 保存下载图片 MemoryStream ms = null; Byte[] buffer = new Byte[myResponse.ContentLength]; int offset = 0, actuallyRead = 0; do { actuallyRead = stream.Read(buffer, offset, buffer.Length - offset); offset += actuallyRead; } while (actuallyRead > 0); ms = new MemoryStream(buffer); byte[] buffurPic = ms.ToArray(); System.IO.File.WriteAllBytes(pathName, buffurPic); #endregion } //异常请求 catch (WebException ex) { } } protected void Method3() { string fileName = @"\upload\" + GenerateTimeStamp() + ".jpg"; string webPath = Server.MapPath(fileName); string pathName = @"d:\" + GenerateTimeStamp() + ".jpg"; HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; HttpWebResponse myResponse = null; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); Stream stream = myResponse.GetResponseStream(); #region 保存下载图片 FileStream fileStream = new FileStream(pathName, FileMode.Create, FileAccess.Write); byte[] bytes = new byte[1024]; int readSize = 0; while ((readSize = stream.Read(bytes, 0, 1024)) > 0) { fileStream.Write(bytes, 0, readSize); fileStream.Flush(); } #endregion myResponse.Close(); stream.Close(); fileStream.Close(); } //异常请求 catch (WebException ex) { } finally { } } public string GenerateTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalMilliseconds).ToString(); } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android开发实现加载网络图片并下载至本地SdCard的方法。分享给大家供大家参考,具体如下:packagecom.example.myima
PHP用代码实现文件下载,阅读PHP用代码实现文件下载,我们一般实现下载都是调用url来下载,但是遇到ie能识别打开的文件就不能用这种方式了,比如下载一个图片、
本文实例为大家分享了OkHttp实现下载图片和上传图片的具体代码,供大家参考,具体内容如下MainActivity.javapublicclassMainAct
本文实例讲述了python实现下载指定网址所有图片的方法。分享给大家供大家参考。具体实现方法如下:#coding=utf-8#downloadpictureso
可以通过更改下载目录为u盘,来实现下载歌曲至u盘。更改目录步骤是: 1、打开酷狗。右上角主菜单。 2、选设置。 3、左边选下载设置。 4、右边选更改目录