说起下载文件,大家都会想起http和FTP下载。http和ftp,其实底层都是基于socket通信,只不过http和ftp协议格式定义不一样而已。
下载文件,要看服务器支持什么类型的协议,如果只支持http,那你就用httpwebrequest类好了,如果支持FTP,那你就用FTP的类来下载文件。
下面是我对两者的总结使用。
http:
/// <summary>下载安装包</summary> public void downloadPack(string macCode ,string appType,string address) { StringBuilder data = new StringBuilder(); data.Append("{\"cpu\":\"" + macCode + "\""); data.Append(",\"appType\":\"" + appType + "\"}"); //data.Append(",\"softwareVersion\":\"" + iniFile.IniReadValue("USER", "versionNo") + "\""); ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address); request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack")); request.Method = "POST"; request.ContentType = "application/json"; request.Accept = "application/json"; byte[] byteData = Encoding.UTF8.GetBytes(data.ToString()); //把要下载的http的文件的相关信息告诉后台 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } Log.AddLog("Download", "2"); sc.start(); process = 0; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { Log.AddLog("Download", "3"); int all_count = 0; using (Stream reader = response.GetResponseStream()) { Log.AddLog("Download", filePath); using (FileStream fw = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { Log.AddLog("Download", "5"); byte[] buffer = new byte[1024 * 10]; while (true) { Log.AddLog("Download", all_count.ToString()); int count = reader.Read(buffer, 0, buffer.Length); all_count += count; fw.Write(buffer, 0, count); sc.setSize(all_count); writeCount += (ulong)count; process = (float)writeCount / (float)fileSize; if (count <= 0) { break; } } isFinish = true; sc.stop(); } } } }
FTP:
static string downloadFile(string fileName) { string error = ""; int sumSize = 0; process = 0; //下载后的文件存放路径 string downloadUrl = getBasePath() + fileName; //需要现在的文件在ftp上的完整路径 string fileUploadPath = ftpServer + fileName; Uri uri = new Uri(fileUploadPath); //创建文件流 FileStream fs = null; Stream responseStream = null; try { //创建一个与FTP服务器联系的FtpWebRequest对象 FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); //设置请求的方法是FTP文件下载 request.Method = WebRequestMethods.Ftp.DownloadFile; //连接登录FTP服务器 request.Credentials = new NetworkCredential(ftpUserName, ftpUserPwd); //获取一个请求响应对象 FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //获取请求的响应流 responseStream = response.GetResponseStream(); //判断本地文件是否存在,如果存在,则打开和重写本地文件 if (File.Exists(downloadUrl)) { fs = File.Open(downloadUrl, FileMode.Open, FileAccess.ReadWrite); } else { fs = File.Create(downloadUrl); } if (fs != null) { int buffer_count = 65536; byte[] buffer = new byte[buffer_count]; int size = 0; while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0) { fs.Write(buffer, 0, size); sumSize += size; process = sumSize * 1.0 / allSize; } fs.Flush(); fs.Close(); responseStream.Close(); } } catch(Exception e) { error = "自动更新服务异常:" + e.ToString(); } finally { if (fs != null) fs.Close(); if (responseStream != null) responseStream.Close(); } return null; }
原文:https://www.cnblogs.com/NangFah/p/14813863.html