Internet and web applications- FTP

第二十七课: 互联网应用程式的第二部分-FTP

 

FTP代表文件传输协议。文件传输协议是一个两台连接互联网电脑之间的文件传输系统。其中一台电脑通常称为伺服器和另外一台则称为客户终端。 FTP程序是非常有用的网站管理系统,网站管理员可以很容易及迅速地把文件上载到网络伺服器以便更新网页。一般的电脑用户, 也可以通过FTP从许多FTP网站下载许多有益的文件,如免费软件,免费游戏,产品信息,应用程式,工具 等等。

FTP 程序通常包括一个显示您的电脑和远程伺服器目录的界面。文件通常可通过点击相关的箭头来传输。要登录到FTP站点,我们通常需要键入用户名称与密码;不过,对于公共领域,我们只需要键入无名氏作为用户名,不必键入密码。FTP主机名称所采取的形式一般是 ftp.servername.com 。举例来说,微软的FTP站点的主机名称是 ftp.microsoft.com 。如果您需要使用FTP软件,您可以购买或者从互联网上一些免费的软件。不过,您也可以建立您使用自己的Visual Basic FTP程式  。 Visual Basic可以让您建立一个功能完整的FTP程序,且可能跟商业FTP软件一样好。FTP背后的引擎是Microsoft Internet Transfer Control 6.0 ,您需要把它插入您的表单来建立FTP程序。Microsoft Internet Transfer Control 6.0的名称是inet,如果你只用一个inet控制,其名称是Inet1
inet1有三个重要的属性,即 inet1.url 用来识别FTP的主机名, inet1.username 用来接受用户名和 inet1.password 用来接受用户的密码。FTP 程式包括阅读伺服器主机名,用户名和密码。这些资料可以输入TextBox1TextBox2 textbox3里,如下所示:

 

Inet1.URL=Text1.Text

Inet1.UserName=Text2.Text

Inet1.Passoword=Text3.Text

用户输入上述资料之后,该程序将用下面的指令试图连接到伺服器上。Execute是方法,DIR是从远程电脑指定的目录阅读的文件列表的FTP指令,你也需要使用getchunk方法去获取`目录里的信息。

  Inet1.Execute, "DIR

连接到服务器之后,您可以使用以下的陈述句从远程电脑选择该文件并下载

Inet1.Execute, , "get" & remotefile & localfile 

Inet1.Execute , , "get" & remotefile & localpath & remotefile

上述的陈述句将确保远程文件将被下载到 localpath 的位置。下载文件和远程文件名称相同。举例来说,远程文件是Readme.txt和localpathC:\temp,所以下载的档案将被保存在C:\temp\readme.txt.

为了监察网络连接的状态,,您可以使用和statechanged事件相关的inet1连同一套下表中列出的常数。

常数

说明

icHostResolvingHost

1

查找主机电脑指定的的IP地址。

icHostResolved

2

成功找到了主机电脑指定的IP地址。

icConnecting

3

正在连接主机电脑。

icConnected

4

成功连接到主机电脑。

icRequesting

5

向主机电脑发出请求。

icRequestSent

6

成功向主机电脑发出请求。

icReceivingResponse

7

正在从主机电脑接受响应讯号。

icResponseReceived

8

成功从主机电脑接受响应讯号。

icDisconnecting

9

跟主机电脑切断联系。

icDisconnected

10

成功跟主机电脑切断联系。

icError

11

在与主机电脑沟通时发生错误。

icResponseCompleted

12

要求已经完成及收到所有数据。

 

 

statechanged 事件里,您可以使用Select Case…End Select陈述句通知用户有关连线的状态。程序如下:

Private Sub Inet1_StateChanged(ByVal State As Integer)

Select Case State

Case icError

MsgBox Inet1.ResponseInfo, , "File failed to transfer"

Case icResolvingHost

Label6.Caption = "Resolving Host"

Case icHostResolved

Label6.Caption = "Host Resolved"

Case icConnecting

Label6.Caption = "Connecting Host"

Case icConnected

Label6.Caption = "Host connected"

Case icReceivingResponse

Label6.Caption = "Receiving Response"

Case icResponseReceived

Label6.Caption = "Got Response"

Case icResponseCompleted

Dim data1 As String

Dim data2 As String

MsgBox "Download Completed"      

End Select

End Sub

 

FTP 程式里,我建立了一个表单和一个对话框。对话框中可以通过按一下该项目菜单栏上来添加,然后在下拉式清单中选择添加表格上的项目。您可以选择一般的对话框,或登录对话框。该对话框是用来接受FTP地址,用户名和密码,然后连接到伺服器器。成功登录后,对话框中将会隐藏而主要表单将被提交供用户浏览远程目录,并选择下载某些文件。

登录对话框的程式:

 

 

Option Explicit

 

Private Sub OKButton_Click()

Inet1.URL = Text1.Text

Inet1.UserName = Text2.Text

Inet1.Password = Text3.Text

Inet1.Execute , "DIR"

Form1.Show

Dialog.Hide

End Sub

 

Private Sub Inet1_StateChanged(ByVal State As Integer)

Select Case State

Case icError

MsgBox Inet1.ResponseInfo, , "File failed to transfer"

Case icResolvingHost

Label6.Caption = "Resolving Host"

Case icHostResolved

Label6.Caption = "Host Resolved"

Case icConnecting

Label6.Caption = "Connecting Host"

Case icConnected

Label6.Caption = "Host connected"

Case icReceivingResponse

Label6.Caption = "Receiving Response"

Case icResponseReceived

Label6.Caption = "Got Response"

Case icResponseCompleted

Dim data As String

Dim data1 As String

 

MsgBox "Transfer Completed"

 Do       

            data1 = Inet1.GetChunk(1024, icString)

            data = data & data1

                   

            Loop While Len(data1) <> 0

            Form1.Text6.Text = data

End Select

End Sub

Private Sub CancelButton_Click()

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

End Sub

FTP 主要界面

 

 

 

[返回主页]