Microsoft .NET Framework 2.0新增加了3个类使我们很方便的对文件传输协议(FTP)服务器进行操作
FtpWebRequest类:实现文件传输协议(FTP)客户端
public sealed class FtpWebRequest : WebRequest
FtpWebResponse类:封装文件传输协议(FTP)服务器对请求的响应
public class FtpWebResponse : WebResponse, IDisposable
WebRequestMethods.Ftp类:表示可与FTP请求一起使用的FTP协议方法的类型,无法继承此类
public static class Ftp
类关系图
操作ftp的一般步骤我总结如下:
第一步:WebRequest.Create方法,获得FtpWebRequest的实例
第二步:利用WebRequestMethods.Ftp设置FtpWebRequest的Method属性,指定使用的FTP协议方法的类型
第三步:设置FtpWebRequest的Credentials属性,指定用户名和密码
第四步:发出请求
第五步:接收响应数据流(有些ftp操作可能没这一步,例如给文件夹改名)
第六步:关闭流
下面从几段代码来分别展示ftp的不同操作:
1.文件夹和文件信息
关键知识说明:
a.FtpWebRequest类没有公开的构造函数,我们通过WebRequest.Create方法,获得FtpWebRequest的实例
b.通过WebRequestMethods.Ftp.ListDirectoryDetails(详细列表)或者WebRequestMethods.Ftp.ListDirectory(简短列表)获取FTP服务器上的文件列表
c.请求返回的数据在GetResponseStream方法返回的流中
d.字符编码请用System.Text.Encoding.Default,要不中文名会乱码
e.FtpWebRequest.Credentials属性设置登陆用户名和密码
f.FtpWebRequest.UseBinary属性,true,指示服务器要传输的是二进制数据.false,指示数据为文本。默认值为true
g.FtpWebRequest.EnableSsl属性,如果控制和数据传输是加密的,则为true.否则为false.默认值为 false
实例代码:
获取ftp://218.16.229.120上的文件信息
Uri uri = new Uri ( "ftp://218.16.229.120" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
//listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
Stream responseStream = listResponse.GetResponseStream ( );
StreamReader readStream = new StreamReader ( responseStream , System.Text.Encoding.Default );
if ( readStream != null )
{
MessageBox.Show ( readStream.ReadToEnd ( ) );
}
MessageBox.Show ( string.Format ( "状态: {0},{1}" ,listResponse.StatusCode, listResponse.StatusDescription ) );
listResponse.Close ( );
responseStream.Close ( );
readStream.Close ( );
通过WebRequestMethods.Ftp.ListDirectoryDetails(详细列表)或者WebRequestMethods.Ftp.ListDirectory(简短列表)返回的结果是不一样的.请看图
利用WebRequestMethods.Ftp.ListDirectoryDetails,readStream.ReadToEnd ( )返回的字符串比较复杂(不同类型的Ftp会有不同返回形式的返回结果),要把里面的文件夹和文件区分别列出来比较繁琐,代码比较多.
大概的调用方法如下:
string dataString = readStream.ReadToEnd ( );
DirectoryListParser parser = new DirectoryListParser ( dataString );
FileStruct [ ] fs = parser.FullListing;
返回的FileStruct有一个属性IsDirectory,可以区分文件夹和文件
DirectoryListParser类代码如下:
using System;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace WindowsApplicationFTP
{
public struct FileStruct
{
public string Flags;
public string Owner;
public bool IsDirectory;
public string CreateTime;
public string Name;
}
public enum FileListStyle
{
UnixStyle,
WindowsStyle,
Unknown
}
public class DirectoryListParser
{
private List<FileStruct> _myListArray;
public FileStruct[] FullListing
{
get
{
return _myListArray.ToArray();
}
}
public FileStruct[] FileList
{
get
{
List<FileStruct> _fileList = new List<FileStruct>();
foreach(FileStruct thisstruct in _myListArray)
{
if(!thisstruct.IsDirectory)
{
_fileList.Add(thisstruct);
}
}
return _fileList.ToArray();
}
}
public FileStruct[] DirectoryList
{
get
{
List<FileStruct> _dirList = new List<FileStruct>();
foreach(FileStruct thisstruct in _myListArray)
{
if(thisstruct.IsDirectory)
{
_dirList.Add(thisstruct);
}
}
return _dirList.ToArray();
}
}
public DirectoryListParser(string responseString)
{
_myListArray = GetList(responseString);
}
private List<FileStruct> GetList(string datastring)
{
List<FileStruct> myListArray = new List<FileStruct>();
string[] dataRecords = datastring.Split('\n');
FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
foreach (string s in dataRecords)
{
if (_directoryListStyle != FileListStyle.Unknown && s != "")
{
FileStruct f = new FileStruct();
f.Name = "..";
switch (_directoryListStyle)
{
case FileListStyle.UnixStyle:
f = ParseFileStructFromUnixStyleRecord(s);
break;
case FileListStyle.WindowsStyle:
f = ParseFileStructFromWindowsStyleRecord(s);
break;
}
if (f.Name != "" && f.Name != "." && f.Name != "..")
{
myListArray.Add(f);
}
}
}
return myListArray;
}
private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
{
///Assuming the record style as
/// 02-03-04 07:46PM <DIR> Append
FileStruct f = new FileStruct();
string processstr = Record.Trim();
string dateStr = processstr.Substring(0,8);
processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
string timeStr = processstr.Substring(0, 7);
processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
f.CreateTime = dateStr + " " + timeStr;
if (processstr.Substring(0,5) == "<DIR>")
{
f.IsDirectory = true;
processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
}
else
{
string[] strs = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
processstr = strs[1];
f.IsDirectory = false;
}
f.Name = processstr; //Rest is name
return f;
}
public FileListStyle GuessFileListStyle(string[] recordList)
{
foreach (string s in recordList)
{
if(s.Length > 10
&& Regex.IsMatch(s.Substring(0,10),"(-|d)((-|r)(-|w)(-|x)){3}"))
{
return FileListStyle.UnixStyle;
}
else if (s.Length > 8
&& Regex.IsMatch(s.Substring(0, 8), "[0-9]{2}-[0-9]{2}-[0-9]{2}"))
{
return FileListStyle.WindowsStyle;
}
}
return FileListStyle.Unknown;
}
private FileStruct ParseFileStructFromUnixStyleRecord(string record)
{
///Assuming record style as
/// dr-xr-xr-x 1 owner group 0 Nov 25 2002 bussys
FileStruct f = new FileStruct();
if (record[0] == '-' || record[0] == 'd')
{// its a valid file record
string processstr = record.Trim();
f.Flags = processstr.Substring(0, 9);
f.IsDirectory = (f.Flags[0] == 'd');
processstr = (processstr.Substring(11)).Trim();
_cutSubstringFromStringWithTrim(ref processstr, ' ', 0); //skip one part
f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
f.CreateTime = getCreateTimeString(record);
int fileNameIndex = record.IndexOf(f.CreateTime)+f.CreateTime.Length;
f.Name = record.Substring(fileNameIndex).Trim(); //Rest of the part is name
}
else
{
f.Name = "";
}
return f;
}
private string getCreateTimeString(string record)
{
//Does just basic datetime string validation for demo, not an accurate check
//on date and time fields
string month = "(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)";
string space = @"(\040)+";
string day = "([0-9]|[1-3][0-9])";
string year = "[1-2][0-9]{3}";
string time = "[0-9]{1,2}:[0-9]{2}";
Regex dateTimeRegex = new Regex(month+space+day+space+"("+year+"|"+time+")", RegexOptions.IgnoreCase);
Match match = dateTimeRegex.Match(record);
return match.Value;
}
private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
{
int pos1 = s.IndexOf(c, startIndex);
string retString = s.Substring(0,pos1);
s = (s.Substring(pos1)).Trim();
return retString;
}
}
}
2.取ftp登陆身份验证完成后的欢迎信息
关键知识说明:
a.FtpWebResponse.WelcomeMessage属性获取身份验证完成时FTP服务器发送的消息
实例代码:
获取ftp://218.16.229.120登陆身份验证完成后的欢迎信息
Uri uri = new Uri ( "ftp://218.16.229.120" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( listResponse.WelcomeMessage );
附加说明:要是FTP服务器的欢迎信息带有中文,运行这段代码时可能会发生异常(基础连接已经关闭: 服务器提交了协议).
解决办法:打补丁Microsoft .NET Framework 2.0 Service Pack 1
3.重命名目录
关键知识说明:
a.WebRequestMethods.Ftp.Rename表示重命名目录的FTP协议方法
b.FtpWebRequest.RenameTo属性重命名的新名称
实例代码:
把ftp://218.16.229.120/上的a目录重命名为av
Uri uri = new Uri ( "ftp://218.16.229.120/a" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.Rename;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
listRequest.RenameTo = "av";
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( listResponse.StatusDescription );
4.删除目录
关键知识说明:
a.WebRequestMethods.Ftp.RemoveDirectory表示移除目录的FTP协议方法
实例代码:
删除ftp://218.16.229.120上的av文件夹
Uri uri = new Uri ( "ftp://218.16.229.120/av" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( listResponse.StatusDescription );
5.新建目录
关键知识说明:
a.WebRequestMethods.Ftp.MakeDirectory表示在FTP服务器上创建目录的协议方法
实例代码:
在ftp://218.16.229.120上建立目录vb
Uri uri = new Uri ( "ftp://218.16.229.120/vb" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( listResponse.StatusDescription );
6.得文件大小
关键知识说明:
a.WebRequestMethods.Ftp.GetFileSize表示要用于检索FTP服务器上的文件大小
b.流数据的长度可以从FtpWebResponse.ContentLength属性中获取。
实例代码:
获取ftp://218.16.229.120上的会议记录.doc文件大小
Uri uri = new Uri ( "ftp://218.16.229.120/会议记录.doc" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.GetFileSize;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( string.Format ( "文件大小: {0}" , listResponse.ContentLength ) );
7.删除文件
关键知识说明:
a.WebRequestMethods.Ftp.DeleteFile表示要用于删除FTP服务器上的文件
实例代码:
删除ftp://218.16.229.120上的工作安排.txt文件
Uri uri = new Uri ( "ftp://218.16.229.120/工作安排.txt" );
FtpWebRequest listRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
listRequest.Method = WebRequestMethods.Ftp.DeleteFile;
string ftpUser = "";
string ftpPassWord = "";
listRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse listResponse = ( FtpWebResponse ) listRequest.GetResponse ( );
MessageBox.Show ( string.Format ( "Delete status: {0}" , listResponse.StatusDescription ) );
8.上传文件
关键知识说明:
a.WebRequestMethods.Ftp.UploadFile表示将文件上载到FTP服务器
b.使用FtpWebRequest对象向服务器上载文件,则必须将文件内容写入请求流,请求流是通过调用FtpWebRequest.GetRequestStream方法.如果未将属性设置为UploadFile,则不能获取流。
c.异步对应方法(FtpWebRequest.BeginGetRequestStream方法和FtpWebRequest.EndGetRequestStream 方法),关于异步上传的实现我会再写在下篇总汇中
实例代码:
上载文件D:\abc.txt到ftp://218.16.229.120上
Stream requestStream = null;
FileStream fileStream = null;
FtpWebResponse uploadResponse = null;
try
{
Uri uri = new Uri ( "ftp://218.16.229.120/abc.txt" );
FtpWebRequest uploadRequest = ( FtpWebRequest ) WebRequest.Create ( uri );
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
string ftpUser = "";
string ftpPassWord = "";
uploadRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
requestStream = uploadRequest.GetRequestStream ( );
fileStream = File.Open ( @"D:\abc.txt" , FileMode.Open );
byte [ ] buffer = new byte [ 1024 ];
int bytesRead;
while ( true )
{
bytesRead = fileStream.Read ( buffer , 0 , buffer.Length );
if ( bytesRead == 0 )
break;
requestStream.Write ( buffer , 0 , bytesRead );
}
requestStream.Close ( );
uploadResponse = ( FtpWebResponse ) uploadRequest.GetResponse ( );
MessageBox.Show ( "Upload complete." );
}
finally
{
if ( uploadResponse != null )
uploadResponse.Close ( );
if ( fileStream != null )
fileStream.Close ( );
if ( requestStream != null )
requestStream.Close ( );
}
其实利用WebClient.UploadData方法,还有一种更简单的上传方法:
WebClient request = new WebClient ( );
string ftpUser = "";
string ftpPassWord = "";
request.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FileStream myStream = new FileStream ( @"D:\abcd.txt" , FileMode.Open , FileAccess.Read );
byte [ ] dataByte = new byte [ myStream.Length ];
myStream.Read ( dataByte , 0 , dataByte.Length ); //写到2进制数组中
myStream.Close ( );
request.UploadData ( "ftp://218.16.229.120/abcd.txt" , dataByte );
9.下载文件
关键知识说明:
a.WebRequestMethods.Ftp.DownloadFile表示要用于从FTP服务器下载文件
b.从FTP服务器下载文件时,如果命令成功,所请求的文件的内容即在响应对象的流中。通过调用FtpWebResponse.GetResponseStream方法,可以访问此流。
实例代码:
从ftp://218.16.229.120上下载文件保存到d:\abc.txt
Stream responseStream = null;
FileStream fileStream = null;
StreamReader reader = null;
try
{
string downloadUrl = "ftp://218.16.229.120/abc.txt";
FtpWebRequest downloadRequest = ( FtpWebRequest ) WebRequest.Create ( downloadUrl );
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
string ftpUser = "";
string ftpPassWord = "";
downloadRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
FtpWebResponse downloadResponse = ( FtpWebResponse ) downloadRequest.GetResponse ( );
responseStream = downloadResponse.GetResponseStream ( );
fileStream = File.Create ( @"d:\" + "abc.txt" );
byte [ ] buffer = new byte [ 1024 ];
int bytesRead;
while ( true )
{
bytesRead = responseStream.Read ( buffer , 0 , buffer.Length );
if ( bytesRead == 0 )
break;
fileStream.Write ( buffer , 0 , bytesRead );
}
MessageBox.Show ( "Download complete" );
}
finally
{
if ( reader != null )
{
reader.Close ( );
}
else
{
if ( responseStream != null )
{
responseStream.Close ( );
}
if ( fileStream != null )
{
fileStream.Close ( );
}
}
}
其实利用WebClient.DownloadData方法,还有一种更简单的下载方法:
Uri uri = new Uri ( "ftp://218.16.229.120/abc.txt" );
WebClient request = new WebClient ( );
string ftpUser = "";
string ftpPassWord = "";
request.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
byte [ ] newFileData = request.DownloadData ( uri.ToString ( ) );
FileStream fs = new FileStream ( @"d:\abc.txt" , FileMode.OpenOrCreate , FileAccess.Write );
fs.Write ( newFileData , 0 , newFileData.Length );
fs.Close ( );
10.2个ftp间传送文件
关键知识说明:
a.在搞懂前面所说下载和上传知识后,其实很好实现2个ftp间传送文件.我们可以把传送文件看成是先下载后上传.把下载的文件响应流数据写到上传文件请求流中即可.
实例代码:
把ftp://218.58.58.19中"集团公司通知"目录中的"080124-成本费用科目调整通知.pdf"文件传送到ftp://218.16.229.120
string downloadUrl = "ftp://218.58.58.19/集团公司通知/080124-成本费用科目调整通知.pdf";
FtpWebRequest downloadRequest = ( FtpWebRequest ) WebRequest.Create ( downloadUrl );
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
string ftpUser = "download";
string ftpPassWord = "download";
downloadRequest.Credentials = new NetworkCredential ( ftpUser , ftpPassWord );
string uploadUrl = "ftp://218.16.229.120/080124-成本费用科目调整通知.pdf";
FtpWebRequest uploadRequest = ( FtpWebRequest ) WebRequest.Create ( uploadUrl );
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
string ftpUser1 = "exwangsoft";
string ftpPassWord1 = "exwangsoft";
uploadRequest.Credentials = new NetworkCredential ( ftpUser1 , ftpPassWord1 );
FtpWebResponse downloadResponse = ( FtpWebResponse ) downloadRequest.GetResponse ( );
Stream responseStream = downloadResponse.GetResponseStream ( );
Stream fileStream = uploadRequest.GetRequestStream ( );
byte [ ] buffer = new byte [ 1024 ];
int bytesRead;
while ( true )
{
//读取ftp://218.58.58.19的响应流数据
bytesRead = responseStream.Read ( buffer , 0 , buffer.Length );
if ( bytesRead == 0 )
break;
//写到ftp://218.16.229.120的请求流数据中
fileStream.Write ( buffer , 0 , bytesRead );
}
fileStream.Close ( );
FtpWebResponse uploadResponse = null;
uploadResponse = ( FtpWebResponse ) uploadRequest.GetResponse ( );
MessageBox.Show ( "complete" );