123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- namespace Falit.Run
- {
- /// <summary>
- /// Web下载文件(单线程下载,速度慢)
- /// </summary>
- public static class WebFileDown
- {
- /// 下载文件方法
- /// 文件保存路径和文件名
- /// 返回服务器文件名
- public static bool DeownloadFile(string sourceFile, string desFile, int dataIndex,
- System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1, System.Windows.Forms.DataGridView dataGridView)
- {
- float percent = 0;
- long sPosition = 0;
- FileStream FStream = null;
- Stream myStream = null;
- string fileName = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1);
- try
- {
- if (File.Exists(desFile))
- {
- FStream = File.OpenWrite(desFile);
- sPosition = FStream.Length;
- long serverFileLength = GetHttpLength(sourceFile);
- if (sPosition == serverFileLength)
- {
- dataGridView.Rows[dataIndex].DefaultCellStyle.ForeColor = Color.Red;
- dataGridView.Rows[dataIndex].Cells["进度"].Value = "100%";
- return true;
- }
- FStream.Seek(sPosition, SeekOrigin.Current);
- }
- else
- {
- //文件不保存创建一个文件
- FStream = new FileStream(desFile, FileMode.Create);
- sPosition = 0;
- }
- HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(sourceFile);
- System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)myRequest.GetResponse();
- if (sPosition > 0)
- {
- myRequest.AddRange(sPosition);
- }
- long totalBytes = myrp.ContentLength;
- myStream = myRequest.GetResponse().GetResponseStream();
- byte[] btContent = new byte[512];
- int intSize = 0;
- long totalDownloadedByte = 0;
- intSize = myStream.Read(btContent, 0, 512);
- while (intSize > 0)
- {
- FStream.Write(btContent, 0, intSize);
- intSize = myStream.Read(btContent, 0, 512);
- totalDownloadedByte = intSize + totalDownloadedByte;
- dataGridView.Rows[dataIndex].Cells["进度"].Value = (float)totalDownloadedByte / (float)totalBytes * 100 + "%";
- percent = (float)totalDownloadedByte / (float)totalBytes * 100;
- label1.Text = percent.ToString("0.00") + "%";
- //System.Windows.Forms.Application.DoEvents();
- }
- return true;
- }
- catch (Exception ex)
- {
- return true;
- }
- finally
- {
- if (myStream != null)
- {
- myStream.Close();
- myStream.Dispose();
- }
- if (FStream != null)
- {
- FStream.Close();
- FStream.Dispose();
- }
- }
- }
- static long GetHttpLength(string url)
- {
- long length = 0;
- try
- {
- var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
- req.Method = "HEAD";
- req.Timeout = 5000;
- var res = (HttpWebResponse)req.GetResponse();
- if (res.StatusCode == HttpStatusCode.OK)
- {
- length = res.ContentLength;
- }
- res.Close();
- return length;
- }
- catch (WebException wex)
- {
- throw wex;
- }
- }
- }
- }
|