.NET1.1开发FTP客户端
来源:岁月联盟
时间:2006-12-27
前面之所以写采用CUTEFTP的引擎做客户端主要是为了方便,在一台已经安装CUTEFTP的PC上使用还是很方便的,但是我们还是希望开发独立的软件。
实际上采用FTP进行文件传输在搞清楚FTP命令和数据连接方式后做起来也不是很难,毕竟FTP是一个公共的协议。
以下是本人写的一个简单的示例,其中的功能只有上传文件,工作模式基本说明了FTP的原理,很容易从例子中推敲出其他FTP的功能。 在近几天的BLOG文章中将陆续推出一些资料信息。
另外,.NET2.0中在System.Net名称空间下已经封装了FTP的几个类,可以直接使用,非常方面,但是考虑到客户端对.net的兼容问题,我还是觉得采用1.1的比较好,虽然费事,但也灵活。
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Net.Sockets;using System.IO; namespace FTPUpLoad{ ///<summary> /// Main 的摘要说明。 /// ///存在的问题 ///1.中文文件名 √ ///2.文件分批读取 √ ///3.进度显示 ///4.扩展到控件中 ///</summary> public class MainForm : System.Windows.Forms.Form { private System.Windows.Forms.TextBox msg; private System.Windows.Forms.Button commBtn; private System.Windows.Forms.Button pathBtn; private System.Windows.Forms.OpenFileDialog openPath; private System.Windows.Forms.TextBox path; private TcpClient tcp; private NetworkStream ns; private System.Windows.Forms.Label process; ///<summary> ///必需的设计器变量。 ///</summary> private System.ComponentModel.Container components = null; public MainForm() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } ///<summary> ///清理所有正在使用的资源。 ///</summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } ///<summary> ///应用程序的主入口点。 ///</summary> [STAThread] static void Main() { Application.Run(new MainForm()); } #region Windows 窗体设计器生成的代码 ///<summary> ///设计器支持所需的方法 - 不要使用代码编辑器修改 ///此方法的内容。 ///</summary> private void InitializeComponent() { this.msg = new System.Windows.Forms.TextBox(); this.path = new System.Windows.Forms.TextBox(); this.commBtn = new System.Windows.Forms.Button(); this.pathBtn = new System.Windows.Forms.Button(); this.openPath = new System.Windows.Forms.OpenFileDialog(); this.process = new System.Windows.Forms.Label(); this.SuspendLayout(); // // msg // this.msg.Location = new System.Drawing.Point(8, 152); this.msg.Multiline = true; this.msg.Name = "msg"; this.msg.Size = new System.Drawing.Size(552, 192); this.msg.TabIndex = 3; this.msg.Text = ""; // // path // this.path.Location = new System.Drawing.Point(8, 8); this.path.Name = "path"; this.path.Size = new System.Drawing.Size(440, 21); this.path.TabIndex = 4; this.path.Text = ""; // // commBtn // this.commBtn.Location = new System.Drawing.Point(232, 48); this.commBtn.Name = "commBtn"; this.commBtn.TabIndex = 5; this.commBtn.Text = "Post"; this.commBtn.Click += new System.EventHandler(this.commBtn_Click); // // pathBtn // this.pathBtn.Location = new System.Drawing.Point(464, 8); this.pathBtn.Name = "pathBtn"; this.pathBtn.TabIndex = 6; this.pathBtn.Text = "浏览…"; this.pathBtn.Click += new System.EventHandler(this.pathBtn_Click); // // openPath // this.openPath.FileOk += new System.ComponentModel.CancelEventHandler(this.openPath_FileOk); // // process // this.process.Location = new System.Drawing.Point(176, 112); this.process.Name = "process"; this.process.Size = new System.Drawing.Size(248, 23); this.process.TabIndex = 7; // // MainForm // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(568, 358); this.Controls.Add(this.process); this.Controls.Add(this.pathBtn); this.Controls.Add(this.commBtn); this.Controls.Add(this.path); this.Controls.Add(this.msg); this.Name = "MainForm"; this.Text = "Main"; this.Load += new System.EventHandler(this.MainForm_Load); this.ResumeLayout(false); } #endregion private void MainForm_Load(object sender, System.EventArgs e) { tcp = new TcpClient("192.168.1.88",21); //输入用户名 WriteNetworkStream(ref ns,"USER Anonymous"); msg.Text += ReadNetworkStream(ref ns); //输入密码 WriteNetworkStream(ref ns,"PASS "); msg.Text += ReadNetworkStream(ref ns); } private void pathBtn_Click(object sender, System.EventArgs e) { openPath.ShowDialog(); } private void openPath_FileOk(object sender, System.ComponentModel.CancelEventArgs e) { path.Text = openPath.FileName; } private void commBtn_Click(object sender, System.EventArgs e) { //刷新服务器当前目录 WriteNetworkStream(ref ns,"CWD /"); msg.Text+=ReadNetworkStream(ref ns); //命令服务器数据模式为被动模式 WriteNetworkStream(ref ns,"PASV"); //获取服务器PASV被动模式打开的数据连接的IP和端口信息 string str = ReadNetworkStream(ref ns); msg.Text+=str; //请求上传文件,此处将命令转换为中文编码的字节数组以支持中文文件名 WriteNetworkStreamChs(ref ns,"STOR "+FileName(path.Text.Trim())); //组合IP和端口 str =str.Substring(str.IndexOf("(")+1,str.IndexOf(")")-str.IndexOf("(")-1); string ip = str.Split(',')[0]+"."+str.Split(',')[1]+"."+str.Split(',')[2]+"."+str.Split(',')[3]; int port = int.Parse(str.Split(',')[4])*256+int.Parse(str.Split(',')[5]); //获取文件流 FileStream fs = File.OpenRead(path.Text.Trim()); //定义一个8字节数组做为缓冲区 byte[] b = new byte[8]; //建立数据传输的TCP连接 TcpClient tcp2 = new TcpClient(ip,port); //建立该连接的网络流 NetworkStream ns2 = tcp2.GetStream(); //每8字节读取文件流中数据到网络流中,并写入到基础存储设备,采用这个方式避免了由于一次性读取大数据量到内存中造成客户机内存使用过量。 while(fs.Read(b,0,b.Length)>0) { //建立数据的TCP连接并将文件流的内容写入到网络流 ns2.Write(b,0,b.Length); ns2.Flush(); } //关闭所用的连接 fs.Close(); ns2.Close(); tcp2.Close(); msg.Text += ReadNetworkStream(ref ns); msg.Text += ReadNetworkStream(ref ns); } #region //功能函数和方法 ///<summary> /// ASCII编码的命令请求 ///</summary> ///<param name="ns">命令传输网络流</param> ///<param name="comm">命令字符串</param> ///<returns></returns> protected string WriteNetworkStream(ref NetworkStream ns,string comm) { ns = tcp.GetStream(); byte[] b = new byte[1024]; b = System.Text.Encoding.ASCII.GetBytes(comm+"/r/n"); ns.Write(b,0,b.Length); ns.Flush(); return string.Empty; } ///<summary> ///中文编码方式的命令请求 ///</summary> ///<param name="ns">命令传输网络流</param> ///<param name="comm">命令字符串</param> ///<returns></returns> protected string WriteNetworkStreamChs(ref NetworkStream ns,string comm) { ns = tcp.GetStream(); byte[] b = new byte[1024]; //将命令以中文编码,以支持中文命令参数 b = System.Text.Encoding.GetEncoding("GB2312").GetBytes(comm+"/r/n"); ns.Write(b,0,b.Length); ns.Flush(); return string.Empty; } ///<summary> ///获取服务器相应字符串 ///</summary> ///<param name="ns">命令传输网络流</param> ///<returns>服务器相应字符串</returns> protected string ReadNetworkStream(ref NetworkStream ns) { ns = tcp.GetStream(); byte[] b = new byte[1024]; ns.Read(b,0,b.Length); ns.Flush(); return System.Text.Encoding.ASCII.GetString(b); } ///<summary> ///获取所选文件路径的文件名 ///</summary> ///<param name="path">文件路径</param> ///<returns>文件名</returns> protected string FileName(string path) { return path.Substring(path.LastIndexOf("//")+1); } #endregion }}