Vb.net FTP上载损坏的文件
我正在使用的程序在上传Excel文件时会损坏这些文件。 该文件在本地计算机上正常,但在远程计算机上,在Excel中打开时,该文件显示已损坏。Excel能够修复该文件,但我不想避免该问题。Vb.net FTP上载损坏的文件,vb.net,ftp,Vb.net,Ftp,我正在使用的程序在上传Excel文件时会损坏这些文件。 该文件在本地计算机上正常,但在远程计算机上,在Excel中打开时,该文件显示已损坏。Excel能够修复该文件,但我不想避免该问题。 到目前为止,我似乎对图像文件没有任何问题 Public Function UploadFile(ByVal User As String, ByVal oFile As FileInfo) As Boolean Dim ftpRequest As FtpWebRequest Dim ftpRes
到目前为止,我似乎对图像文件没有任何问题
Public Function UploadFile(ByVal User As String, ByVal oFile As FileInfo) As Boolean
Dim ftpRequest As FtpWebRequest
Dim ftpResponse As FtpWebResponse
Try
FtpCheckAndCreateDir(User)
ftpRequest = CType(FtpWebRequest.Create(Base + User + "/" + oFile.Name), FtpWebRequest)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
ftpRequest.Credentials = Cred ' New NetworkCredential(...)
ftpRequest.KeepAlive = KeepAlive ' false
ftpRequest.EnableSsl = UseSSL ' false
If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length) As Byte
Using fr As FileStream = oFile.OpenRead
fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length))
End Using
Using writer As Stream = ftpRequest.GetRequestStream
writer.Write(fileContents, 0, fileContents.Length)
End Using
ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
Return True
Catch ex As WebException
Return False
End Try
End Function
编辑:所以我拿了一个文件,通过免费的咖啡杯ftp上传,下载后,它就可以正常打开了。
我用我的程序上传文件,然后用咖啡杯下载,当我试图在excel中打开它时,发现它已损坏。
我使用HxD来比较文件,它返回的消息是:“所选文件相同。但是文件大小不同!”当我对两个文件运行校验和时,它们返回的值不同 我真的不知道如何解决这个问题,也不知道我可以研究什么来找到答案。
如果需要,我可以提供文件。因此,经过大量搜索和查找,我找到了以下答案: 我可以用它来上传文件而不被破坏 最后的ftp功能将旧部件注释掉,以便于查看更改:
Public Function UploadFile(ByVal User As String, ByVal oFile As FileInfo) As Boolean
Dim ftpRequest As FtpWebRequest
Dim ftpResponse As FtpWebResponse
Try
FtpCheckAndCreateDir(User)
ftpRequest = CType(FtpWebRequest.Create(Base + User + "/" + oFile.Name), FtpWebRequest)
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
ftpRequest.Credentials = Cred
ftpRequest.KeepAlive = KeepAlive
ftpRequest.EnableSsl = UseSSL
If UseSSL Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
Dim fileContents(oFile.Length) As Byte
Using fr As FileStream = oFile.OpenRead
Using writer As Stream = ftpRequest.GetRequestStream
fr.CopyTo(writer) ' This is the important part.
'writer.Write(fileContents, 0, fileContents.Length)
End Using
'fr.Read(fileContents, 0, Convert.ToInt32(oFile.Length))
End Using
'Using writer As Stream = ftpRequest.GetRequestStream
' writer.Write(fileContents, 0, fileContents.Length)
'End Using
ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
ftpResponse.Close()
ftpRequest = Nothing
Return True
Catch ex As WebException
Return False
End Try
End Function
问题在于您的文件编写器,重要的是要注意,图像在丢失信息时非常宽容 我希望这个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
公共类FTP
“---------------------------[BroCode]--------------------------
“------------------------------FTP-----------------------------
专用\u凭据作为System.Net.NetworkCredential
Sub New(ByVal _FTPUser作为字符串,ByVal _FTPPass作为字符串)
设置凭据(\u FTPUser,\u FTPPass)
端接头
公共子上传文件(ByVal\u文件名为字符串,ByVal\u上传路径为字符串)
Dim\u FileInfo作为新的System.IO.FileInfo(\u FileName)
Dim_FtpWebRequest As System.Net.FtpWebRequest=CType(System.Net.FtpWebRequest.Create(新Uri(_UploadPath)),System.Net.FtpWebRequest)
_FtpWebRequest.Credentials=\u凭证
_FtpWebRequest.KeepAlive=False
_FtpWebRequest.Timeout=20000
_FtpWebRequest.Method=System.Net.WebRequestMethods.Ftp.UploadFile
_FtpWebRequest.UseBinary=True
_FtpWebRequest.ContentLength=\u FileInfo.Length
长度为整数=2048
Dim buff(buffLength-1)作为字节
Dim_FileStream As System.IO.FileStream=_FileInfo.OpenRead()
尝试
Dim_Stream As System.IO.Stream=_FtpWebRequest.GetRequestStream()
Dim contentLen As Integer=\u FileStream.Read(buff,0,buffLength)
当内容为0时执行此操作
_Stream.Write(buff、0、contentLen)
contentLen=\u FileStream.Read(buff,0,buffLength)
环
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_Dispose()文件流
特例
MessageBox.Show(例如Message,“上载错误:”,MessageBoxButtons.OK,MessageBoxIcon.Error)
结束尝试
端接头
公共子下载文件(ByVal\u文件名为字符串,ByVal\u ftpDownloadPath为字符串)
尝试
Dim请求为System.Net.FtpWebRequest=System.Net.WebRequest.Create(ftpDownloadPath)
_request.KeepAlive=False
_request.Method=System.Net.WebRequestMethods.Ftp.DownloadFile
_request.Credentials=\u凭证
Dim_响应为System.Net.FtpWebResponse=_request.GetResponse()
Dim RESPOSSESTREAM As System.IO.Stream=\u response.GetResponseStream()
将fs设置为新的System.IO.FileStream(_FileName,System.IO.FileMode.Create)
responseStream.CopyTo(fs)
responseStream.Close()
_答复:Close()
特例
MessageBox.Show(例如Message,“下载错误:”,MessageBoxButtons.OK,MessageBoxIcon.Error)
结束尝试
端接头
公共函数GetDirectory(ByVal _ftpPath作为字符串)作为列表(字符串)
Dim ret作为新列表(字符串)
尝试
Dim请求为System.Net.FtpWebRequest=System.Net.WebRequest.Create(_ftpPath)
_request.KeepAlive=False
_request.Method=System.Net.WebRequestMethods.Ftp.ListDirectoryDetails
_request.Credentials=\u凭证
Dim_响应为System.Net.FtpWebResponse=_request.GetResponse()
Dim RESPOSSESTREAM As System.IO.Stream=\u response.GetResponseStream()
Dim_reader As System.IO.StreamReader=新的System.IO.StreamReader(responseStream)
将文件数据设置为字符串=\u reader.ReadToEnd
Dim Lines()作为String=FileData.Split(新字符串(){Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)
对于每行中的字符串
ret.Add(l)
下一个
_reader.Close()
_答复:Close()
特例
Show(例如Message,“目录提取错误:”,MessageBoxButtons.OK,MessageBoxIcon.Error)
结束尝试
回程网
端函数
私有子集凭据(ByVal\u FTPUser作为字符串,ByVal\u FTPPass作为字符串)
_凭据=新系统.Net.NetworkCredential(\u FTPUser,\u FTPPass)
端接头
末级
@Richard\u Grant你好!你真好,为我去年九月自己回答的一个问题提供了另一个答案!这听起来可能有点刺耳,但主要是因为你对我的答案的评论在我半年前第一次给出答案时让人觉得很恼火。话虽如此,尽管存在误解,但您的答案非常完整,尽管我还没有尝试过(因为我的解决方案已经起作用),但它看起来相当不错。我相信整堂课都会对某人有用。