HttpWebRequest &&FileStream分块读取和写入文件&WebClient
2016-08-21 15:30
225 查看
//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);
//设置接收对象的范围为0-10000000字节。
hwr.AddRange(0, 10000000);
using (Stream stream = hwr.GetResponse().GetResponseStream())
{
//文件流,流信息读到文件流中,读完关闭
using (FileStream fs = System.IO.File.Create(PhysicalFilePath + "\\" + fileName))
{
//建立字节组,并设置它的大小是多少字节
byte[] bytes = new byte[10240];
int n = 1;
while (n > 0)
{
//一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
n = stream.Read(bytes, 0, 10240);
fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
}
}
}
}
}
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分块读取和写入文件
static void Main(string[] args)
{
FileStream fsOutput = new FileStream("D:\\wwwrite.txt", FileMode.Create | FileMode.Append);
FileStream fs;
//获得文件所在路径
string filePath = "C:\\rrr.txt";
//打开文件
try
{
fs = new FileStream(filePath, FileMode.Open);
}
catch (Exception)
{
throw;
}
int chunkSize = 100;
//尚未读取的文件内容长度
long left = fs.Length;
//存储读取结果
byte[] bytes = new byte[chunkSize];
//读取位置
int start = 0;
//实际返回结果长度
int num = 0;
//当文件未读取长度大于0时,不断进行读取
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);
}
//开始写入
fsOutput.Write(bytes, 0, bytes.Length);
//清空缓冲区
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();
}
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);
//设置接收对象的范围为0-10000000字节。
hwr.AddRange(0, 10000000);
using (Stream stream = hwr.GetResponse().GetResponseStream())
{
//文件流,流信息读到文件流中,读完关闭
using (FileStream fs = System.IO.File.Create(PhysicalFilePath + "\\" + fileName))
{
//建立字节组,并设置它的大小是多少字节
byte[] bytes = new byte[10240];
int n = 1;
while (n > 0)
{
//一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
n = stream.Read(bytes, 0, 10240);
fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
}
}
}
}
}
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分块读取和写入文件
static void Main(string[] args)
{
FileStream fsOutput = new FileStream("D:\\wwwrite.txt", FileMode.Create | FileMode.Append);
FileStream fs;
//获得文件所在路径
string filePath = "C:\\rrr.txt";
//打开文件
try
{
fs = new FileStream(filePath, FileMode.Open);
}
catch (Exception)
{
throw;
}
int chunkSize = 100;
//尚未读取的文件内容长度
long left = fs.Length;
//存储读取结果
byte[] bytes = new byte[chunkSize];
//读取位置
int start = 0;
//实际返回结果长度
int num = 0;
//当文件未读取长度大于0时,不断进行读取
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);
}
//开始写入
fsOutput.Write(bytes, 0, bytes.Length);
//清空缓冲区
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();
}
相关文章推荐
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- httpWebRequest获取流和WebClient的文件抓取
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件[转]
- httpWebRequest获取流和WebClient的文件抓取
- 文件上传 content-type:application/json System.Web.HttpContext.Current.Request.InputStream 读取不到数据
- C# 读取网页源码的三种办法WebClient、WebRequest、HttpWebRequest
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件[转]
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- 使用WebClient或HttpWebRequest模拟上传文件和数据
- java web 利用 commons-fileupload-1.2 实现文件上传 解决 中文乱码& 非文件属性的读取
- HttpWebRequest实现读取天气预报信息
- C#使用浏览器(Internet Explorer)默认的代理服务器设置(WebRequest,WebClient,HttpWebRequest)
- webrequest与webclient 下载文件,显示进度(访问网页)
- HttpWebRequest实现文件下载(断点续传)
- C#在WinForm下使用HttpWebRequest上传文件并显示进度