最近,分配给我一个项目,这个项目已成功运转,在这个项目中我需要允许我们的用户经由XML和FTP向另一个公司输出它们的数据。在研究了所有的ASP.NET书籍以后,我们发现任何地方都没有FTP方法,并且花费了一些时间在因特网上搜索相关的例子。我弄清楚了那一点。网络没有自产的FTP支持。我开始去信心并且以A.被迫使用一些旧的ASP代码和Com或者B.停止下来并且购买某些部件。
不过,我最终偶然发现在Microsoft的MSDN中已经发表过处理FTP的一个完整的类。在这个类中的代码有一点冗长,所以对Microsoft的声誉有影响,雇员必须停下来并且将它们砍去。
‘在Visual Basic.NET 2002中进行调用。你会
‘将数据移动8位。在Visual Basic .NET 2002中,你必须
‘这个数乘以2的8次方
‘端口=parts(4)*(2^8)
‘进行调用,并且接着以Visual Basic .NET 2003进行注释当前行。
port = parts(4) << 8
我们还使用Visual Studio 2002,所以得到这个注释是一件好事。
为了测试这个类,我建立了默认的FTP网页,以允许匿名用户进行存取。
图1
图2
之后,通过代码调用是件轻而易举的事。
Dim ftpClient As New clsFTP("localhost", "", "anonymous", "", 80)
If (ftpClient.Login() = True) Then
‘创建一个新文件夹
ftpClient.CreateDirectory("FTPFOLDERNEW")
‘将新的文件夹设置为活动文件夹。
ftpClient.ChangeDirectory("FTPFOLDERNEW")
‘设置FTP模式
ftpClient.SetBinaryMode(True)
‘从你的硬盘上上载一个文件到FTP网页
ftpClient.UploadFile(Server.MapPath("SampleFile.xml"))
‘对上载文件重命名
ftpClient.RenameFile("SampleFile.xml", "SampleFile_new.xml")
‘删除一个文件
ftpClient.DeleteFile("SampleFile_new.xml")
‘总是关闭链接,确保没有任何不在使用中的FTP链接
‘检查你是否登录到FTP服务器,并且接着关闭链接
ftpClient.CloseConnection()
End If
浏览ASP.NET论坛,我发现对ASP.NET特征的一个论坛,在那里一个用户注释到拥有一个自己的FTP库该是多么地不错呀。保罗.威尔逊,一个ASP的内行,答复到它已开始2.0了。非常明确它是受欢迎的。
以下是在MSDN文章中找到的对FTP客户端类的完整代码。怎么使用Visual Basic .NET来存取一个文件传输协议网页呢?
‘这个类允许你使用Visual Basic.NET对FTP网页执行直接链接。
‘这个类支持以下FTP命令:
‘-Upload 一个文件
‘-Download一个文件
‘-Create 一个目录
‘-Remove 一个目录
‘-改变目录
‘-Remove 一个文件
‘-Rename 一个文件
‘-Set 远程用户的用户名
‘-Set 远程用户的密码
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Sockets
‘FTP 类
Public Class clsFTP
#Region "Class Variable Declarations"
Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String
Private m_sRemotePassword, m_sMess As String
Private m_iRemotePort, m_iBytes As Int32
Private m_objClientSocket As Socket
Private m_iRetValue As Int32
Private m_bLoggedIn As Boolean
Private m_sMes, m_sReply As String
‘设置用户来对FTP服务器读取和写入数据的数据包的大小
‘对下列具体大小
Public Const BLOCK_SIZE = 512
Private m_aBuffer(BLOCK_SIZE) As Byte
Private ASCII As Encoding = Encoding.ASCII
Public flag_bool As Boolean
‘普通变量定义
Private m_sMessageString As String
#End Region
#Region "Class Constructors"
‘Main类的构造器
Public Sub New()
m_sRemoteHost = "microsoft"
m_sRemotePath = "."
m_sRemoteUser = "anonymous"
m_sRemotePassword = ""
m_sMessageString = ""
m_iRemotePort = 21
m_bLoggedIn = False
End Sub
‘参数化的构造器
Public Sub New(ByVal sRemoteHost As String, _
ByVal sRemotePath As String, _
ByVal sRemoteUser As String, _
ByVal sRemotePassword As String, _
ByVal iRemotePort As Int32)
m_sRemoteHost = sRemoteHost
m_sRemotePath = sRemotePath
m_sRemoteUser = sRemoteUser
m_sRemotePassword = sRemotePassword
m_sMessageString = ""
m_iRemotePort = 21
m_bLoggedIn = False
End Sub
#End Region
#Region "Public Properties"
‘设置或得到你想链接的FTP服务器的名称
Public Property RemoteHostFTPServer() As String
‘得到FTP服务器的名称
Get
Return m_sRemoteHost
End Get
‘设置FTP服务器的名称
Set(ByVal Value As String)
m_sRemoteHost = Value
End Set
End Property
‘设置或得到你想链接的FTP服务器的FTP端口
Public Property RemotePort() As Int32
‘得到FTP端口号
Get
Return m_iRemotePort
End Get
‘设置FTP端口数号
Set(ByVal Value As Int32)
m_iRemotePort = Value
End Set
End Property
‘设置或得到你想链接的FTP服务器的远程路径
Public Property RemotePath() As String
‘得到远程路径
Get
Return m_sRemotePath
End Get
‘设置远程路径
Set(ByVal Value As String)
m_sRemotePath = Value
End Set
End Property
‘设置你想链接的远程FTP服务器的密码
Public Property RemotePassword() As String
Get
Return m_sRemotePassword
End Get
Set(ByVal Value As String)
m_sRemotePassword = Value
End Set
End Property
‘设置或得到你想链接远程的FTP服务器的用户
Public Property RemoteUser() As String
Get
Return m_sRemoteUser
End Get
Set(ByVal Value As String)
m_sRemoteUser = Value
End Set
End Property
‘设置messagestring类
Public Property MessageString() As String
Get
Return m_sMessageString
End Get
Set(ByVal Value As String)
m_sMessageString = Value
End Set
End Property
#End Region
#Region "Public Subs and Functions"
‘从文件系统中返回一个文件列表。在string()函数中返回文件。
Public Function GetFileList(ByVal sMask As String) As String()
Dim cSocket As Socket
Dim bytes As Int32
Dim seperator As Char = ControlChars.Lf
Dim mess() As String
m_sMes = ""
‘检查你是否登录到FTP服务器上
If (Not (m_bLoggedIn)) Then
Login()
End If
cSocket = CreateDataSocket()
‘发送FTP命令
SendCommand("NLST " & sMask)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
m_sMes = ""
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop
mess = m_sMes.Split(seperator)
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return mess
End Function
‘得到FTP服务器上的文件大小
Public Function GetFileSize(ByVal sFileName As String) As Long
Dim size As Long
If (Not (m_bLoggedIn)) Then
Login()
End If
‘发送一个FTP命令
SendCommand("SIZE " & sFileName)
size = 0
If (m_iRetValue = 213) Then
size = Int64.Parse(m_sReply.Substring(4))
Else
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return size
End Function
‘登录FTP服务器
Public Function Login() As Boolean
m_objClientSocket = _
New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim ep As New IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList(0), m_iRemotePort)
Try
m_objClientSocket.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to the remote server")
End Try
ReadReply()
If (m_iRetValue <> 220) Then
CloseConnection()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
‘为了发送一个对服务器的用户登录ID,发送一个FTP命令
SendCommand("USER " & m_sRemoteUser)
If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
If (m_iRetValue <> 230) Then
‘为了发送一个对服务器的用户密码,发送一个FTP命令
SendCommand("PASS " & m_sRemotePassword)
If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
End If
m_bLoggedIn = True
‘为了改变映射的远程服务器的文件夹的目录,调用用户定义的ChangeDirectory函数
ChangeDirectory(m_sRemotePath)
‘返回最终结果
Return m_bLoggedIn
End Function
‘如果模式值为真,对下载设置为二进制模式。否则,设置为ASCII模式
Public Sub SetBinaryMode(ByVal bMode As Boolean)
If (bMode) Then
‘发送FTP命令,设置为二进制模式
‘(TYPE是一种用作说明请求类型的FTP命令.)
SendCommand("TYPE I")
Else
‘发送FTP命令,设置ASCII模式。
‘(TYPE是一种用作说明请求类