c#实现 ftp http共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
2012-10-17 15:03
961 查看
转载地址: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; } }
相关文章推荐
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
- C#访问远程服务器共享目录下载文件到本地
- C# 从FTP服务器下载文件到本地
- 以命令方式从FTP 服务器下载文件到本地指定目录
- php实现从ftp服务器上下载文件树到本地电脑的程序
- 以命令方式从FTP 服务器下载文件到本地指定目录
- C# Http方式下载文件到本地
- C#实现从FTP服务器下载压缩文件并解压.
- php实现从ftp服务器上下载文件树到本地电脑的程序
- C# Http方式下载文件到本地类改进版
- Delphi7 中利用Indy的TidFtp客户端控件实现在服务器指定目录下下载需要的文件
- HttpWebRequest 下载网页Html代码 下载文件(Remote和FTP)Get方式
- Android使用ftp方式实现文件上传和下载
- C# 实现在线软件自动升级自动下载更新文件
- 实现FTP服务器文件下载
- Java连接FTP服务器并且实现对其文件的上传和下载
- C#实现文件下载的几种方式
- C#实现文件下载的几种方式
- Windows下配置ftp服务器,使用QT实现文件上传和下载