我正在从我的数据库中读取数据并将其重新格式化为逗号分隔的CSV文件,我需要将其上传到FTP服务器。
此过程读取包含逗号分隔格式数据的字符串的Byte数组,并将其上载到服务器:
Private Sub uploadToServer(ByVal data() As Byte, ByVal filename As String)
Try
Dim ftpr As FtpWebRequest = _
DirectCast(WebRequest.Create(String.Format("ftp://ftp.this.com/{0}", filename)), FtpWebRequest)
ftpr.Credentials = New NetworkCredential("thisUser", "somePassword0rOther")
ftpr.Method = WebRequestMethods.Ftp.UploadFile
Using stream As Stream = ftpr.GetRequestStream()
stream.Write(data, 0, data.Length)
End Using
Catch ex As Exception
el.WriteEntry(String.Format("Error uploading file: {1}", vbNewLine & ex.Message))
End Try
End Sub
至少,它应该做什么......以及它昨天做了什么。现在,我继续在异常中返回:
远程服务器返回错误:227进入被动模式
尝试使用FileZilla连接到同一服务器时,我遇到了同样的错误,这让我觉得问题出在服务器上。
我不打算在此处解决服务器问题,但我需要的是确认此代码没有任何问题。然后我可以去serverfault并查询那里可能存在的服务器问题。
那么......这段代码有什么问题吗?
修改
测试其他访问FTP的方法我发现:
FileZilla返回Connection timed out. Failed to retrieve directory listing
通过Windows资源管理器连接返回An error occurred opening that folder on the FTP Server. Make sure you have permission to access that folder. Details: The operation timed out
。当办公室中的任何人试图通过FTP访问服务器时也会发生同样的情况。
我很确定这也是我的应用程序中发生的事情。
答案 0 :(得分:0)
根据NCFtp's Documentation,这是请求服务器进入被动模式时的相应响应。我认为你可能会在不应该的时候将响应解释为错误。
此外,根据Wikipedia's FTP return code entry,所有以2xx开头的回复都是积极回应;他们表示成功。具体来说,227
表示成功转换为被动模式。
答案 1 :(得分:0)
227进入被动模式是对FTP PASV命令的有效响应。我不确定为什么我们会将此作为例外。可能是你的防火墙。
此外,您还没有正确构建FTP,请查看我的FTP类:非常简单。
Public Class FTP
'-------------------------[BroCode]--------------------------
'----------------------------FTP-----------------------------
Private _credentials As System.Net.NetworkCredential
Sub New(ByVal _FTPUser As String, ByVal _FTPPass As String)
setCredentials(_FTPUser, _FTPPass)
End Sub
Public Sub UploadFile(ByVal _FileName As String, ByVal _UploadPath As String)
Dim _FileInfo As New System.IO.FileInfo(_FileName)
Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
_FtpWebRequest.Credentials = _credentials
_FtpWebRequest.KeepAlive = False
_FtpWebRequest.Timeout = 20000
_FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
_FtpWebRequest.UseBinary = True
_FtpWebRequest.ContentLength = _FileInfo.Length
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Try
Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
Do While contentLen <> 0
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub DownloadFile(ByVal _FileName As String, ByVal _ftpDownloadPath As String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpDownloadPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim fs As New System.IO.FileStream(_FileName, System.IO.FileMode.Create)
responseStream.CopyTo(fs)
responseStream.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Download Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function GetDirectory(ByVal _ftpPath As String) As List(Of String)
Dim ret As New List(Of String)
Try
Dim _request As System.Net.FtpWebRequest = System.Net.WebRequest.Create(_ftpPath)
_request.KeepAlive = False
_request.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
_request.Credentials = _credentials
Dim _response As System.Net.FtpWebResponse = _request.GetResponse()
Dim responseStream As System.IO.Stream = _response.GetResponseStream()
Dim _reader As System.IO.StreamReader = New System.IO.StreamReader(responseStream)
Dim FileData As String = _reader.ReadToEnd
Dim Lines() As String = FileData.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
For Each l As String In Lines
ret.Add(l)
Next
_reader.Close()
_response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Directory Fetch Error: ", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Return ret
End Function
Private Sub setCredentials(ByVal _FTPUser As String, ByVal _FTPPass As String)
_credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
End Sub
End Class
初始化:
Dim ftp As New FORM.FTP("username", "password")
ftp.UploadFile("c:\file.jpeg", "ftp://domain/file.jpeg")
ftp.DownloadFile("c:\file.jpeg", "ftp://ftp://domain/file.jpeg")
Dim directory As List(Of String) = ftp.GetDirectory("ftp://ftp.domain.net/")
ListBox1.Items.Clear()
For Each item As String In directory
ListBox1.Items.Add(item)
Next