如何使用VB.net从大型机数据集上传和下载文件
我拥有大型机数据集的所有FTP详细信息。
答案 0 :(得分:2)
在我工作的地方,我们发现您可以在System.Net命名空间中使用FtpWebRequest类。它的使用非常简单。
答案 1 :(得分:1)
您应该可以使用WebRequest - 请参阅this post。
答案 2 :(得分:0)
以下是从大型机上传和下载的示例
#Form1.vb
#====
Public Class Form1
Dim myftp As New ftpClient()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strReturnFields As String = ""
Dim strName As String
Dim strPWD As String
Dim strftpLogin As String
'FieldsChek(strReturnFields)
If strReturnFields.Length > 0 Then
MessageBox.Show(strReturnFields + " cannot be empty")
Return
End If
strName = userName.Text
strPWD = Password.Text
strftpLogin = Server.Text
myftp.LogInFTP(strName, strPWD, strftpLogin)
txtMessage.Text = myftp.GetReturnNameMessage
txtMessage.Text = txtMessage.Text + myftp.GetReturnPwdMessage
End Sub
Private Sub btnFTPUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFTPUpload.Click
Dim strUPFilePath As String
Dim strFtpAddress As String
Dim strMessage As String
strUPFilePath = txtFilePath.Text
strFtpAddress = txtDestFile.Text
myftp.FTPUpLoad(strUPFilePath, strFtpAddress, strMessage)
txtMessage.Text += vbCrLf + strMessage
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim strFile As String = txtDestFile.Text
Dim strReturnMessage As String
strReturnMessage = myftp.DeleteFile(strFile)
txtMessage.Text += vbCrLf + strReturnMessage
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strReturnMessage As String
strReturnMessage = myftp.QuitFTP(strReturnMessage)
txtMessage.Text += vbCrLf + strReturnMessage
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
#===============
#ftpCreate.vb
#=============
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class ftpCreate
Private ftpTcpClient As TcpClient
Public ResponseStream As NetworkStream
Public ReturnNameMessage As String
Public ReturnPwdMessage As String
Public Sub ftpLogin(ByVal strName As String, ByVal strPWD As String, ByVal strftpLogin As String)
Try
Dim strCommand As String
Dim strReturnMessage As String
Dim bteSendBytes() As Byte
'Dim bteSendBytes As [Byte]()
Dim bteRetruenBytes() As Byte
Dim intReturnByteLength As Integer
Dim ftpTcpClient As TcpClient = New TcpClient(strftpLogin, 21)
ResponseStream = ftpTcpClient.GetStream
strCommand = "USER " + strName + vbCrLf
bteSendBytes = System.Text.Encoding.ASCII.GetBytes(strCommand)
ResponseStream.Write(bteSendBytes, 0, bteSendBytes.Length)
intReturnByteLength = ftpTcpClient.ReceiveBufferSize
ReDim bteRetruenBytes(intReturnByteLength)
ResponseStream.Read(bteRetruenBytes, 0, intReturnByteLength)
strReturnMessage = Encoding.ASCII.GetString(bteRetruenBytes) + "/ "
ReturnNameMessage = strCommand + strReturnMessage
Thread.Sleep(1000)
strCommand = "PASS " + strPWD + vbCrLf
Array.Clear(bteSendBytes, 0, bteSendBytes.Length)
bteSendBytes = Encoding.ASCII.GetBytes(strCommand)
ResponseStream.Write(bteSendBytes, 0, bteSendBytes.Length)
intReturnByteLength = ftpTcpClient.ReceiveBufferSize
Thread.Sleep(1000)
ReDim bteRetruenBytes(intReturnByteLength)
ResponseStream.Read(bteRetruenBytes, 0, intReturnByteLength)
strReturnMessage = Encoding.ASCII.GetString(bteRetruenBytes) + "/ "
ReturnPwdMessage = strCommand + strReturnMessage + vbCrLf
strCommand = "TYPE " + "E" + vbCrLf
Array.Clear(bteSendBytes, 0, bteSendBytes.Length)
bteSendBytes = Encoding.ASCII.GetBytes(strCommand)
ResponseStream.Write(bteSendBytes, 0, bteSendBytes.Length)
intReturnByteLength = ftpTcpClient.ReceiveBufferSize
Thread.Sleep(1000)
ReDim bteRetruenBytes(intReturnByteLength)
ResponseStream.Read(bteRetruenBytes, 0, intReturnByteLength)
strReturnMessage = Encoding.ASCII.GetString(bteRetruenBytes) + "/ "
ReturnPwdMessage = strCommand + strReturnMessage + vbCrLf
Catch ex As SocketException
ReturnPwdMessage = ex.Message
End Try
End Sub
End Class
#====
#ftpClient.vb
#====
Imports System.Net
Imports System.IO
Imports System.Net.Sockets
Imports System.Text
Public Class ftpClient
Dim ReturnNameMessage
Dim ReturnPwdMessage
Dim myftpCreate As ftpCreate
Public Sub LogInFTP(ByVal strName As String, ByVal strPWD As String, ByVal strftpLogin As String)
myftpCreate = New ftpCreate()
myftpCreate.ftpLogin(strName, strPWD, strftpLogin)
ReturnNameMessage = myftpCreate.ReturnNameMessage
ReturnPwdMessage = myftpCreate.ReturnPwdMessage
End Sub
Public ReadOnly Property GetReturnNameMessage() As String
Get
Return ReturnNameMessage
End Get
End Property
Public ReadOnly Property GetReturnPwdMessage() As String
Get
Return ReturnPwdMessage
End Get
End Property
Public Sub FTPUpLoad(ByVal strFilePath As String, ByVal strFtpPath As String, ByRef pstrReturnMessage As String)
Dim UPFile As New FileStream(strFilePath, FileMode.Open)
Dim bytUPFile() As Byte
Dim outstream As String
Dim lngFileLength As Long
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
Dim UpLoadStream As NetworkStream
Dim BLOCK_SIZE As Integer = 512
Dim buffer As Byte() = New Byte(BLOCK_SIZE - 1) {}
Dim ASCII As Encoding = Encoding.ASCII
Try
lngFileLength = UPFile.Length
ReDim bytUPFile(lngFileLength)
UPFile.Read(bytUPFile, 0, lngFileLength)
outstream = Encoding.ASCII.GetString(bytUPFile)
bytUPFile = Encoding.ASCII.GetBytes(outstream)
FTPCommands(ftpStream, "PASV", returnMessage)
UpLoadStream = GetConnectTcpClient(returnMessage)
FTPCommands(ftpStream, "TYPE A", returnMessage)
FTPCommands(ftpStream, "STOR " + strFtpPath, returnMessage)
pstrReturnMessage += returnMessage.TrimEnd
'UpLoadStream.Write(outstream, 0, lngFileLength)
UpLoadStream.Write(bytUPFile, 0, lngFileLength)
UpLoadStream.Close()
UPFile.Close()
Catch ex As Exception
pstrReturnMessage = ex.Message
End Try
End Sub
Public Sub FTPDownLoad(ByVal strFilePath As String, ByVal strFtpPath As String, ByRef pstrReturnMessage As String)
Dim UPFile As New FileStream(strFtpPath, FileMode.Create)
Dim bytUPFile() As Byte
Dim lngFileLength As Long
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "PASV", returnMessage)
Dim DownloadStream As NetworkStream
DownloadStream = GetConnectTcpClient(returnMessage)
FTPCommands(ftpStream, "TYPE I", returnMessage)
FTPCommands(ftpStream, "RETR " + strFilePath, returnMessage)
pstrReturnMessage += returnMessage
ReDim bytUPFile(1024)
Do
lngFileLength = DownloadStream.Read(bytUPFile, 0, 1024)
UPFile.Write(bytUPFile, 0, lngFileLength)
Loop While lngFileLength > 0
UPFile.Close()
DownloadStream.Close()
End Sub
Private Function FTPCommands(ByVal ftpStream As NetworkStream, ByVal strCommand As String, ByRef strMessage As String) As Integer
Dim bteCommand() As Byte
bteCommand = Encoding.ASCII.GetBytes(strCommand + vbCrLf)
ftpStream.Write(bteCommand, 0, bteCommand.Length)
Dim b(360000) As Byte
ftpStream.Read(b, 0, 360000)
strMessage = Encoding.ASCII.GetString(b)
End Function
Private Function GetConnectTcpClient(ByVal respMessage As String) As NetworkStream
Dim i As Integer
Dim strIP As String
Dim strIPs() As String
Dim strAddress As String
Dim intPort As Integer
Try
strIP = respMessage.Substring(respMessage.IndexOf("(")).Replace("(", "")
strIPs = strIP.Split(",")
strIP = ""
For i = 0 To 3
strIP += strIPs(i) + "."
Next
strAddress = strIP.Substring(0, strIP.Length - 1)
intPort = Integer.Parse(strIPs(4)) * 256 + _
Integer.Parse(strIPs(5).Substring(0, strIPs(5).IndexOf(")")))
Dim dataClient As New TcpClient()
Dim cIPEP As New IPEndPoint(IPAddress.Parse(strAddress), intPort)
dataClient.Connect(cIPEP)
Return dataClient.GetStream
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Public Function CreateDir(ByVal strDirPath As String) As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "MKD " + strDirPath, returnMessage)
returnMessage = "Creating Dir" + vbCrLf + returnMessage
Return returnMessage
ftpStream.Close()
End Function
Public Function DeleteDir(ByVal strDirPath As String) As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "RMD " + strDirPath, returnMessage)
returnMessage = "Delete Dir" + vbCrLf + returnMessage
Return returnMessage
ftpStream.Close()
End Function
Public Function PrintDir() As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "PWD ", returnMessage)
returnMessage = returnMessage
Return returnMessage
ftpStream.Close()
End Function
Public Function ChangeDir(ByVal strDirPath As String) As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "CWD " + strDirPath, returnMessage)
returnMessage = returnMessage
Return returnMessage
ftpStream.Close()
End Function
Public Function DeleteFile(ByVal strFile As String) As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "DELE " + strFile, returnMessage)
returnMessage = returnMessage
Return returnMessage
ftpStream.Close()
End Function
Public Function QuitFTP(ByVal strFile As String) As String
Dim ftpStream As NetworkStream = myftpCreate.ResponseStream
Dim returnMessage As String
FTPCommands(ftpStream, "QUIT", returnMessage)
returnMessage = returnMessage
Return returnMessage
ftpStream.Close()
End Function
End Class