2

我正在尝试通过 FTP 从我的本地计算机将文件上传到 FTP 服务器,该服务器目前也在我的本地计算机上。我有这个我正在打电话的潜艇:

    Public Sub UploadFTPFile(ByVal ftpservername, ByVal fullfilepath, ByVal filename, ByVal username, ByVal password)

    Dim clsRequest As System.Net.FtpWebRequest = _
    DirectCast(System.Net.WebRequest.Create("ftp://" & ftpservername & "/" & filename), System.Net.FtpWebRequest)
    clsRequest.Credentials = New System.Net.NetworkCredential(username, password)
    clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

    clsRequest.KeepAlive = False

    ' read in file...
    Dim bFile() As Byte = System.IO.File.ReadAllBytes(fullfilepath)

    ' upload file...
    Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()
    clsStream.Write(bFile, 0, bFile.Length)
    clsStream.Close()
    clsStream.Dispose()
End Sub

调用 sub 时,我这样做:

UploadFTPFile("192.168.1.3/Temp", selectedSoundFileLong, OpenFileDialog.SafeFileName, "", "")

换句话说,在 Sub 中,这个字符串:

DirectCast(System.Net.WebRequest.Create("ftp://" & ftpservername & "/" & filename), System.Net.FtpWebRequest)

, 创建以下内容:

    DirectCast(System.Net.WebRequest.Create("ftp://192.168.1.3/Temp/test.mp3"), System.Net.FtpWebRequest)

在子的这一行:

        Dim clsStream As System.IO.Stream = _
        clsRequest.GetRequestStream()

发生此错误:

The remote server returned an error: (550) File unavailable

这可能是什么原因?我正在使用免费软件 Golder FTP Server 运行 FTP 服务器。我认为它设置正确,因为使用与上面使用 Windows Explorer 完全相同的字符串连接到 FTP 服务器效果很好。

4

3 回答 3

2

尝试翻转UsePassive属性。控制和数据在 FTP 中使用不同的端口,有可能您在控制端口上通过,但在数据端口上以某种方式被阻止。

于 2009-10-31T18:16:04.717 回答
2

您确定 webRequest 字符串中没有多余的空格吗?如果字符串碰巧像“ ftp://192.168.1.3/Temp/test.mp3 ”,我相信您会收到此错误另外,请检查以确保您具有写入该服务器的正确权限。此外,请确保您的文件是 test.mp3 而不是 test.MP3。

于 2009-10-31T18:17:56.330 回答
0

550 是 System.Net.FtpWebRequest 连接错误代码。

此外,您还没有正确构建您的 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:23:42.870 回答