您的位置:首页 > 编程语言 > PHP开发

雷击程序(二)------通过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文件是否存在。
程序代码如下所示:
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;

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: