【问题标题】:Uploading a file via FtpWebRequest The remote server returned an error 227 entering passive mode通过 FtpWebRequest 上传文件远程服务器返回错误 227 进入被动模式
【发布时间】:2013-03-09 15:54:43
【问题描述】:

我正在从我的数据库中读取数据并将其重新格式化为一个逗号分隔的 CSV 文件,我需要将其上传到 FTP 服务器。

此过程读取包含逗号分隔格式数据的字符串的字节数组并将其上传到服务器:

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 访问服务器时,也会发生同样的情况。

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

【问题讨论】:

    标签: vb.net upload ftp ftpwebrequest


    【解决方案1】:

    根据NCFtp's Documentation,这是请求服务器进入被动模式时的适当响应。我认为您可能将响应解释为错误,而您不应该这样做。

    另外,根据Wikipedia's FTP return code entry,所有以2xx开头的回复都是正面回复;它们表明成功。具体来说,227 表示成功过渡到被动模式。

    【讨论】:

    • 我是这么想的,但是如果 227 是好消息,是什么导致了异常?
    • 输出字符串的内容:The remote server returned an error。我在您的代码中的任何地方都没有看到。代码在哪里跳转到异常?也许该消息与真正导致异常的原因无关。
    • 在 Catch 块被命中之前执行的最后一行是 stream.Write(data, 0, data.Length)。基本上,尝试将文件写入服务器。不过这在星期一有效......
    • 正如您在帖子中指出的那样,感觉就像是服务器问题。您能否让其他人尝试使用不同的 FTP 客户端访问服务器,看看他们是否遇到相同的错误?另外 - 您是否更改了任何代码,因为它以前有效?
    • 我已经用一些测试结果更新了这个问题。此外,自从我在星期一(当它起作用时)写它以来,那个特定的程序并没有改变
    【解决方案2】:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      相关资源
      最近更新 更多