我正在尝试通过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资源管理器使用与上面完全相同的字符串连接到FTP服务器效果很好。
答案 0 :(得分:2)
尝试翻转UsePassive属性。控制和数据使用different ports in FTP,您可能已经通过控制端口,但在数据端口上以某种方式被阻止。
答案 1 :(得分:2)
你确定webRequest字符串中没有额外的空格吗?我相信如果字符串恰好是“ftp://192.168.1.3/ Temp / test.mp3”,你会收到此错误。另外,请检查以确保您具有写入该服务器的正确权限。此外,请确保您的文件是test.mp3而不是test.MP3。
答案 2 :(得分: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