现在的位置: 首页 > 综合 > 正文

c#实现 ftp http共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载

2013年10月17日 ⁄ 综合 ⁄ 共 2715字 ⁄ 字号 评论关闭

转载地址:http://www.cnblogs.com/Fengsp/archive/2012/08/06/2625149.html

c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载

近段时间在研究这个下载 在网上找了一些列子看了看 决定把这几种下载综合起来发一个……

复制代码
//从ftp服务器上下载文件的功能  
        public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination)  
        {  
            FtpWebRequest reqFTP;  
            try  
            {  
                FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create);  
                // 根据uri创建FtpWebRequest对象   
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));  
                // 指定执行什么命令  
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
                // 默认为true,连接不会被关闭  
                reqFTP.UseBinary = true;  
                // ftp用户名和密码  
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
                FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse();  
                Stream ftpStream = ftpresponse.GetResponseStream();  
                long cl = ftpresponse.ContentLength;  
                int bufferSize = 2048;  
                int readCount;  
                byte[] buffer = new byte[bufferSize];  
                readCount = ftpStream.Read(buffer, 0, bufferSize);  
                while (readCount > 0)  
                {  
                    outputStream.Write(buffer, 0, readCount);  
                    readCount = ftpStream.Read(buffer, 0, bufferSize);  
                }  
                ftpStream.Close();  
                outputStream.Close();  
                ftpresponse.Close();  
            }  
            catch (Exception ex)  
            {  
  
  
            }  
        }  
        //从http服务器上下载文件的功能  
        //为服务器路径server_path   
        //本地存储路径local_path  
        public void Download(string server_path, string local_path)  
        {  
            HttpWebRequest req;  
            try  
            {  
                string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));  
                if (!Directory.Exists(folder))  
                    Directory.CreateDirectory(folder);  
                // 根据uri创建FtpWebRequest对象   
                req = (HttpWebRequest)HttpWebRequest.Create(new Uri(server_path));  
                // 指定执行什么命令  
                req.Method = WebRequestMethods.Http.Get;  
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();  
                if (File.Exists(local_path))  
                {  
                    FileInfo localFile = new FileInfo(local_path);  
                    if (localFile.LastWriteTime > response.LastModified) return;  
                }  
                FileStream outputStream = new FileStream(local_path, FileMode.Create);  
                Stream stream = response.GetResponseStream();  
                long cl = response.ContentLength;  
                int bufferSize = 2048;  
                int readCount;  
                byte[] buffer = new byte[bufferSize];  
                readCount = stream.Read(buffer, 0, bufferSize);  
                while (readCount > 0)  
                {  
                    outputStream.Write(buffer, 0, readCount);  
                    readCount = stream.Read(buffer, 0, bufferSize);  
                }  
                stream.Close();  
                outputStream.Close();  
                response.Close();  
            }  
            catch (Exception ex)  
            {  
                string s = ex.Message;  
            }  
        }  
  
  
  
  
        //从共享文件夹下载  
        public void Download(string server_path, string local_path, int i)  
        {  
            try  
            {  
                server_path = "\\" + server_path;  
                string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));  
                if (!Directory.Exists(folder))  
                    Directory.CreateDirectory(folder);  
                if (!File.Exists(server_path)) return;  
  
  
                //服务器文件信息  
                FileInfo serverInfo = new FileInfo(server_path);  
                DateTime serverTime = serverInfo.LastWriteTime;  
                if (File.Exists(local_path))  
                {  
                    FileInfo localinfo = new FileInfo(local_path);  
                    DateTime localTime = localinfo.LastWriteTime;  
  
  
                    if (localTime >= serverTime)  
                        return;  
                }  
  
  
                serverInfo.CopyTo(local_path, true);  
                return;  
            }  
            catch (Exception ex)  
            {  
                string s = ex.Message;  
            }  
        }
复制代码
【上篇】
【下篇】

抱歉!评论已关闭.