HttpWebRequest FileStream分块读取和写入文件WebClient
//HttpWebRequest??下載文件
private void DownloadFile(string filePath)
? ? ? ? ? {
? ? ? ? ? ? ??string[] temp = filePath.Split(new string[] { @"/" }, StringSplitOptions.None);
? ? ? ? ? ? ? string fileName = temp[temp.Length - 1];
? ? ? ? ? ? ? string PhysicalFilePath = Request.PhysicalApplicationPath + ConfigurationManager.AppSettings["UploadFilePath"];
? ? ? ? ? ? ? if (!Directory.Exists(PhysicalFilePath))
? ? ? ? ? ? ? ? ? Directory.CreateDirectory(PhysicalFilePath);
? ? ? ? ? ? ? if (!System.IO.File.Exists(PhysicalFilePath + "\\" + fileName))
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? Uri downUri = new Uri(@"http://192.168.1.1" + filePath);
? ? ? ? ? ? ? ? ? HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
?? //設(shè)置接收對(duì)象的范圍為0-10000000字節(jié)。
? ? ? ? ? ? ? ? ? ? ? hwr.AddRange(0, 10000000);
? ? ? ? ? ? ? ? ? using (Stream stream = hwr.GetResponse().GetResponseStream())
? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? //文件流,流信息讀到文件流中,讀完關(guān)閉
? ? ? ? ? ? ? ? ? ? ? using (FileStream fs = System.IO.File.Create(PhysicalFilePath + "\\" + fileName))
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? //建立字節(jié)組,并設(shè)置它的大小是多少字節(jié)
? ? ? ? ? ? ? ? ? ? ? ? ? byte[] bytes = new byte[10240];
? ? ? ? ? ? ? ? ? ? ? ? ? int n = 1;
? ? ? ? ? ? ? ? ? ? ? ? ? while (n > 0)
? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //一次從流中讀多少字節(jié),并把值賦給N,當(dāng)讀完后,N為0,并退出循環(huán)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? n = stream.Read(bytes, 0, 10240);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fs.Write(bytes, 0, n); //將指定字節(jié)的流信息寫(xiě)入文件流中
? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? }
? ? ? ? ? }
static public void HttpRemoteDownload2()
? ? ? ? {
? ? ? ? ? ? string URLAddress = "http://192.168.1.106/ttIP.txt";
? ? ? ? ? ? HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLAddress);
? ? ? ? ? ? request.Method = "GET";
? ? ? ? ? ? HttpWebResponse response = (HttpWebResponse)request.GetResponse();
? ? ? ? ? ? Stream responseStream = response.GetResponseStream();
? ? ? ? ? ? List<byte> btlst = new List<byte>();
? ? ? ? ? ? int b = responseStream.ReadByte();
? ? ? ? ? ? while (b > -1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? btlst.Add((byte)b);
? ? ? ? ? ? ? ? b = responseStream.ReadByte();
? ? ? ? ? ? }
? ? ? ? ? ? byte[] bts = btlst.ToArray();
? ? ? ? ? ? File.WriteAllBytes("testDownload.txt", bts); // 保存文件
? ? ? ? }
//FileStream分塊讀取和寫(xiě)入文件
?static void Main(string[] args)
? ? ? ? {? ? ? ? ? ? FileStream fsOutput = new FileStream("D:\\wwwrite.txt", FileMode.Create | FileMode.Append);
? ? ? ? ? ? FileStream fs;
? ? ? ? ? ? //獲得文件所在路徑
? ? ? ? ? ? string filePath = "C:\\rrr.txt";
? ? ? ? ? ? //打開(kāi)文件
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? fs = new FileStream(filePath, FileMode.Open);
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ? ? ? ? int chunkSize = 100;
? ? ? ? ? ? //尚未讀取的文件內(nèi)容長(zhǎng)度
? ? ? ? ? ? long left = fs.Length;
? ? ? ? ? ? //存儲(chǔ)讀取結(jié)果
? ? ? ? ? ? byte[] bytes = new byte[chunkSize];
? ? ? ? ? ?
? ? ? ? ? ? //讀取位置
? ? ? ? ? ? int start = 0;
? ? ? ? ? ? //實(shí)際返回結(jié)果長(zhǎng)度
? ? ? ? ? ? int num = 0;
? ? ? ? ? ? //當(dāng)文件未讀取長(zhǎng)度大于0時(shí),不斷進(jìn)行讀取
? ? ? ? ? ? while (left > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? fs.Position = start;
? ? ? ? ? ? ? ? num = 0;
? ? ? ? ? ? ? ? if (left < chunkSize)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? bytes = new byte[left];
? ? ? ? ? ? ? ? ? ? num = fs.Read(bytes, 0, Convert.ToInt32(left));
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? num = fs.Read(bytes, 0, chunkSize);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? //開(kāi)始寫(xiě)入
? ? ? ? ? ? ? ? ? ? fsOutput.Write(bytes, 0, bytes.Length);
? ? ? ? ? ? ? ? ? ? //清空緩沖區(qū)
? ? ? ? ? ? ? ? ? ? fsOutput.Flush();? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ?? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? Array.Clear(bytes, 0, bytes.Length);
? ? ? ? ? ? ? ? if (num == 0)
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? start += num;
? ? ? ? ? ? ? ? left -= num;
? ? ? ? ? ? ? ? Console.WriteLine(Encoding.UTF8.GetString(bytes));
? ? ? ? ? ? }
? ? ? ? ??
? ? ? ? ? ? Console.ReadLine();
? ? ? ? ? ? fs.Close();
? ? ? ? ? ? fsOutput.Close();
? ? ? ? }
//WebClient
static ?public void HttpRemoteDownload()
? ? ? ? {
? ? ? ? ? ? string URLAddress = "http://192.168.1.106/ttIP.txt";
? ? ? ? ? ? WebClient client = new WebClient();
? ? ? ? ? ? Stream str = client.OpenRead(URLAddress);
? ? ? ? ? ? StreamReader reader = new StreamReader(str);
? ? ? ? ? ? byte[] fileData = new byte[1000000];
? ? ? ? ? ? int fileDataLength = (int)fileData.Length;
? ? ? ? ? ? int startIndex = 0;
? ? ? ? ? ? while (fileDataLength > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int m = str.Read(fileData, startIndex, fileDataLength);
? ? ? ? ? ? ? ? if (m == 0)
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? startIndex += m;
? ? ? ? ? ? ? ? fileDataLength -= m;
? ? ? ? ? ? }
? ? ? ? ? ? reader.Dispose();
? ? ? ? ? ? str.Dispose();
? ? ? ? ? ? string receivePath = @"C:\";
? ? ? ? ? ? string path = receivePath + System.IO.Path.GetFileName(URLAddress);
? ? ? ? ? ? FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
? ? ? ? ? ? fstr.Write(fileData, 0, startIndex);
? ? ? ? ? ? fstr.Flush();
? ? ? ? ? ? fstr.Close();
? ? ? ? }
總結(jié)
以上是生活随笔為你收集整理的HttpWebRequest FileStream分块读取和写入文件WebClient的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: thrift如何定义Java中的obje
- 下一篇: 读《现代软件工程——构建之法》有感