网络文件操作

本文介绍了一种通过C#实现的网络文件处理方法,包括获取网络IP和端口、获取文件大小、多线程下载文件以及合并文件的具体步骤和技术细节。

0、获取网络IP、端口:

            NameValueCollection nameValueTable = new NameValueCollection();

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
                nameValueTable = HttpUtility.ParseQueryString(queryString);
            }

 nameValueTable["IP"];

1、获取网络文件大小:

                HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.Method = "GET";
                httpWebRequest.KeepAlive = false;
                httpWebRequest.AllowAutoRedirect = true;
                httpWebRequest.Referer = url.Substring(0, url.LastIndexOf("/") + 1);
                httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.2;)";
                WebResponse webResponse = httpWebRequest.GetResponse();
                Int64 fileSize = webResponse.ContentLength;
                webResponse.Close();
                httpWebRequest.Abort();
                httpWebRequest = null;
        

 2、下载文件

         Byte[] bytes = new Byte[this.bufferSize];
              Int32 readBytes = 0;
              FileStream fs = new FileStream(threadFileName[currentThreadIndex], FileMode.Create, FileAccess.Write);
          
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpWebRequest.Method = "GET"; httpWebRequest.KeepAlive = false; httpWebRequest.AllowAutoRedirect = true; httpWebRequest.Referer = url.Substring(0, url.LastIndexOf("/") + 1); httpWebRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.2;)"; //接收的起始位置及接收的长度 httpWebRequest.AddRange(Convert.ToInt32(threadFileStart[currentThreadIndex]), Convert.ToInt32(threadFileStart[currentThreadIndex] + threadFileSize[currentThreadIndex] - 1)); WebResponse webResponse = httpWebRequest.GetResponse(); Stream stream = webResponse.GetResponseStream();//获得接收流 readBytes = stream.Read(bytes, 0, this.bufferSize); while (readBytes > 0) { fs.Write(bytes, 0, readBytes); readBytes = stream.Read(bytes, 0, this.bufferSize); } fs.Flush(); fs.Close(); fs.Dispose(); stream.Close(); stream.Dispose(); webResponse.Close(); httpWebRequest.Abort(); httpWebRequest = null;

 3、合并文件(一般要加锁(Lock))

             FileStream fs = new FileStream(destinationFileFullName, FileMode.Append, FileAccess.Write);
                    FileStream fsTemp = null;
                    Int32 readBytes;
                    Byte[] bytes = new Byte[this.bufferSize];
                    for (Int32 idx = 1; idx < threadCount; idx++) //从第二个线程接收的临时文件开始合并
                    {
                        fsTemp = new FileStream(threadFileName[idx], FileMode.Open, FileAccess.Read);
                        readBytes = fsTemp.Read(bytes, 0, this.bufferSize);

                        while (readBytes > 0)
                        {
                            fs.Write(bytes, 0, readBytes);
                            readBytes = fsTemp.Read(bytes, 0, this.bufferSize);
                        }

                        fsTemp.Close();
                        File.Delete(threadFileName[idx]);
                    }

                    fs.Close();
                    fs.Dispose();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值