我正在尝试修复另一个开发人员开发的程序。我想要解决的是每次程序尝试上传文件时都会返回“文件”。找不到(代码= 550)'。奇怪的是,实际文件由于某种原因实际上传...当我收到修复代码的副本时,我自己尝试了程序并且它给了我同样的错误但文件实际上没有上传。 / p>
代码Imports Utilities.FTP(我在网上找不到很多文档..)并且在运行ftp.CopyToFTP()之后抛出异常。 CopyToFTP基本上将文件复制到您放置的FTP站点目录。
有没有人有办法解决这个问题?还是解决方案?
Private Sub FtpOutputFile(ByVal atts As List(Of String)) Dim inStream As StreamReader Dim parseLn()As String Dim ftp As New Utilities.FTP.FTP()
Try
inStream = New StreamReader(File.OpenRead(Me.Directory & "ftp.dat"))
'Setup FTP object
While inStream.EndOfStream = False
parseLn = Split(inStream.ReadLine(), ",")
Select Case parseLn(0)
Case "Destination"
ftp.Destination = parseLn(1)
Case "Expiration"
ftp.Expiration = parseLn(1)
Case "Host"
ftp.FTP_Host = parseLn(1)
Case "Password"
ftp.FTP_Password = parseLn(1)
Case "Timeout"
ftp.FTP_Timeout = parseLn(1)
Case "UserName"
ftp.FTP_UserName = parseLn(1)
End Select
End While
inStream.Close()
Dim MyEnum As IEnumerator(Of String) = atts.GetEnumerator
While MyEnum.MoveNext 'I added this? Before it was just the directory but this would be the entire file?
ftp.Source = Path.GetDirectoryName(MyEnum.Current) + "\DailyExcel" & DailyDate.ToString("yyyyMMdd") & ".csv"
If Not ftp.CopyToFTP() Then
Throw New Exception(ftp.ErrorMessage)
End If
End While
Catch ex As Exception
Trace.WriteLine(Date.Now.ToString("MM/dd/yyyy HH:mm") & ": " & ex.Message & vbNewLine & ex.StackTrace)
MsgBox(ex.Message())
End Try
End Sub
我的.dat文件如下:
Destination,excelfiles/Test/
Expiration,30
Host,myftp.mydomainname.com
Password,password
Timeout,5000
UserName,username
答案 0 :(得分:0)
FTP错误代码550被拒绝访问。您确定服务器配置正确吗?
答案 1 :(得分:0)
感谢Sam Makin,我能够找出错误。该文件似乎将从FTP目录下载所有数据,并删除最近修改日期超过30天的所有文件。如果它没有将它添加到要与之比较的数组中,则与本地目录交叉引用以确定应上载哪些文件。
问题在于它下载了包含文件的所有文件'。'和' ..'据我所知,我将假设这意味着当前目录和父目录。所以它会看到该目录是在一段时间之前创建的,试图删除它,它会说该文件没有找到。
答案 2 :(得分:0)
我不知道你编码的所有帽子废话,但有一些规则, 出于安全性和效率原因,您不应在FTP(或任何实例化的连接类型)期间生成文件,以便将实时时间降至绝对最小值。
基本上,除了编程人员适应的程序风格之外,我所能看到的其他任何东西在代码中都是政治上的错误。
550具体是访问被拒绝错误。这不是整个错误消息,它还应该告诉您它是否是与被拒绝的服务器或用户凭据的连接。
您的团队似乎并不熟悉VB.net中的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