雷击程序(二)------通过ftp协议下载文本文件
2011-05-07 05:37
274 查看
要通过ftp协议下载文本文件,先要建立ftp服务,模拟现场环境。
创建ftp服务,可以使用软件方式,也可以使用windows提供的方式。
使用windows方式,首先要安装IIS下的ftp服务,界面如下图所示。
然后在自己机器上创建一个ftp服务,创建过程如下界面所示。
点击完成这样一个ftp服务就创建成功了。
要用代码实现从ftp服务器上下载文本文件,网上有许多写好的代码,只要下载来改成自己需要的就ok了。
要连接ftp服务器需要的参数主要有:ftp服务器IP地址,用户名和密码。
this.ftpServerIP = ftpServerIP;
this.ftpUserID = ftpUserID;
this.ftpPassword = ftpPassword;
程序中主要用到了两个函数,一个是从ftp服务器下载文件。另一个 用于判断ftp文件是否存在。
程序代码如下所示:
创建ftp服务,可以使用软件方式,也可以使用windows提供的方式。
使用windows方式,首先要安装IIS下的ftp服务,界面如下图所示。
然后在自己机器上创建一个ftp服务,创建过程如下界面所示。
点击完成这样一个ftp服务就创建成功了。
要用代码实现从ftp服务器上下载文本文件,网上有许多写好的代码,只要下载来改成自己需要的就ok了。
要连接ftp服务器需要的参数主要有:ftp服务器IP地址,用户名和密码。
this.ftpServerIP = ftpServerIP;
this.ftpUserID = ftpUserID;
this.ftpPassword = ftpPassword;
程序中主要用到了两个函数,一个是从ftp服务器下载文件。另一个 用于判断ftp文件是否存在。
程序代码如下所示:
namespace LightningStroke { public class FtpUpDown { string ftpServerIP; string ftpUserID; string ftpPassword; FtpWebRequest reqFTP; /// <summary> /// 连接ftp /// </summary> /// <param name="path">url</param> private void Connect(String path) { // 根据uri创建FtpWebRequest对象 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); // 指定数据传输类型 reqFTP.UseBinary = true; // ftp用户名和密码 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); } public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword) { this.ftpServerIP = ftpServerIP; this.ftpUserID = ftpUserID; this.ftpPassword = ftpPassword; } /// <summary> /// 从ftp服务器下载文件 /// </summary> /// <param name="remotePath">远程服务器上文件存放路径</param> /// <param name="remoteFileName">文件名称</param> /// <param name="localFilePath">文件在本地存放路径</param> public void Download(string remotePath, string remoteFileName, string localFilePath) { FileStream fs = null; string url = "ftp://" + ftpServerIP+remotePath+ remoteFileName; Connect(url);//连接 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream responseStream = response.GetResponseStream(); //判断本地文件是否存在,如果存在,则打开和重写本地文件 string localFilePathName = localFilePath + @"/" + remoteFileName; if (File.Exists(localFilePathName)) { fs = File.Open(localFilePathName, FileMode.Open, FileAccess.ReadWrite); } else { fs = File.Create(localFilePathName); } 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); } fs.Flush(); fs.Close(); responseStream.Close(); } } /// <summary> /// 判断ftp服务器上是否存在要下载的文件 /// </summary> /// <param name="uri"></param> /// <param name="fileName">要下载的文件名</param> /// <returns></returns> public bool findFileExist(string uri, string ftpDirectory, string fileName) { StringBuilder result = new StringBuilder(); try { Connect(uri); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名 string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("/n"); line = reader.ReadLine(); } result.Remove(result.ToString().LastIndexOf("/n"), 1); reader.Close(); response.Close(); string strResult = result.ToString(); //if (File.Exists(ftpPathFileName)) // return true; //else // return false; if (strResult.IndexOf(fileName) != -1) return true; else return false; } catch (Exception) { return false; } } } }
相关文章推荐
- 通过程序实现文件下载更名,动态文件下载
- 通过USB转TTL串口下载stm32程序
- keil 通过JTAG下载程序 报错:error: flash download failed - "cortex-m3"的解决方法
- Postman chrome浏览器插件下载地址和安装方式,解决“只能通过Chrome网上应用商店安装该程序”的方法
- 通过新立得下载的软件安装程序文件位置和Ubuntu .deb包安装方法
- 通过一般处理程序实现【文件下载】
- 通过程序,而不是连接地址下载文件
- 夺命雷公狗---微信开发21----通过程序进行下载多媒体文件
- 发布程序 通过safari直接下载手机安装
- 通过程序 VB.Net 或 C# 读取文本文件行数
- PHP通过header实现文本文件的下载
- mini2440通过dnw下载linux虚拟机的程序
- DM355通过串口下载程序方法步骤 ,davinci,串口
- 如果在PC机上安装了S3C2440驱动但是通过虚拟机不能用DNW下载程序
- 通过minicom,下载程序到Imote2
- PHP通过header实现文本文件的下载
- arm通过tftp从ubuntu主机上下载可执行程序
- 移植rzsz通过串口下载程序到开发板上的工具
- 在IAR下通过Jlink将程序直接下载到Flash指定地址
- 通过keil给stm32板下载程序出现cannot load flash programming algorithm