C# 通过 FTP 上传文件 - 服务器返回错误 (550) 文件不可用,找不到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1655104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upload file via FTP - Server returned error (550) File is unavailable, cannot find file
提问by Kenny Bones
I'm trying to upload a file via FTP from my local computer to an FTP server, which is also on my local computer at the moment. I've got this sub I'm calling:
我正在尝试通过 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
When calling the sub, I do it like this:
调用 sub 时,我这样做:
UploadFTPFile("192.168.1.3/Temp", selectedSoundFileLong, OpenFileDialog.SafeFileName, "", "")
In other words, in the Sub, this string:
换句话说,在 Sub 中,这个字符串:
DirectCast(System.Net.WebRequest.Create("ftp://" & ftpservername & "/" & filename), System.Net.FtpWebRequest)
, creates the following:
, 创建以下内容:
DirectCast(System.Net.WebRequest.Create("ftp://192.168.1.3/Temp/test.mp3"), System.Net.FtpWebRequest)
And at this line in the sub:
在子中的这一行:
Dim clsStream As System.IO.Stream = _
clsRequest.GetRequestStream()
This error occurs:
出现此错误:
The remote server returned an error: (550) File unavailable
What could be the cause of this? I'm running an FTP Server using Golder FTP Server, which is freeware. I think it's setup correctly because connecting to the FTP Server using the exact same string as above using Windows Explorer works great.
这可能是什么原因?我正在使用免费软件 Golder FTP Server 运行 FTP 服务器。我认为它设置正确,因为使用 Windows 资源管理器使用与上面完全相同的字符串连接到 FTP 服务器效果很好。
采纳答案by northpole
are you sure there is no extra white space in the webRequest string? I believe you would get this error if the string happened to be like "ftp://192.168.1.3/Temp/test.mp3" Also, check to make sure you have the correct privileges to write to that server. Additionally, make sure you file is test.mp3 and not test.MP3.
您确定 webRequest 字符串中没有多余的空格吗?我相信如果字符串碰巧类似于“ ftp://192.168.1.3/Temp/test.mp3”,您会收到此错误此外,请检查以确保您具有写入该服务器的正确权限。此外,请确保您的文件是 test.mp3 而不是 test.MP3。
回答by robertc
Try flipping the UsePassiveproperty. Control and data use different ports in FTP, it's possible you're getting through on the control port but getting blocked somehow on the data port.
尝试翻转UsePassive属性。控制和数据在 FTP 中使用不同的端口,您可能在控制端口上通过,但在数据端口上以某种方式被阻止。
回答by THE AMAZING
550 is a System.Net.FtpWebRequest connection error code.
550 是 System.Net.FtpWebRequest 连接错误代码。
Also you have not constructed your FTP correctly check out my FTP class: Its pretty straight forward.
此外,您还没有正确构建您的 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
To initialize:
初始化:
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