2

我正在从我的数据库中读取数据并将其重新格式化为一个逗号分隔的 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 Entering Passive Mode

我在尝试使用 FileZilla 连接到同一台服务器时遇到了同样的错误,这让我认为问题出在服务器上。

我不打算在这里解决服务器问题,但我确实需要确认此代码没有任何问题。然后我可以去 serverfault 并在那里查询任何可能的服务器问题。

所以......这段代码有什么问题吗?

编辑
测试访问我发现的 FTP 的其他方式:

  • 通过 RDP 将 FTP 访问到服务器本身(Windows Server 2008 R2)可以工作
  • 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 访问服务器时,也会发生同样的情况。

我很确定这也是我的应用程序中正在发生的事情。

4

2 回答 2

0

根据NCFtp 的文档,这是请求服务器进入被动模式时的适当响应。我认为您可能会在不应该的情况下将响应解释为错误。

另外,根据维基百科的 FTP 返回码条目,所有以 2xx 开头的响应都是肯定响应;它们表明成功。具体来说,227表示成功过渡到被动模式。

于 2013-03-20T13:58:15.957 回答
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
于 2015-02-23T09:20:40.593 回答