在FTP客户端中获取远程文件大小时出错 [英] error in getting remote file size in FTP client
本文介绍了在FTP客户端中获取远程文件大小时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个简单的ftp客户端来下载文件,该文件还显示下载进度.因此,为此,我需要知道远程文件的大小...但是当我使用以下代码时,它将返回-1 ... MsgBox(cl.ToString)返回-1
Hi,
I''m trying to create a simple ftp client to download a file which shows the download progress also. So for this i need to know the size of remote file...but it''s returning -1 when i use the following code... MsgBox(cl.ToString) Returning -1
Private Sub Download(ByVal filePath As String, ByVal fileName As String)
FTPSettings.IP = "xxxx.com/IBIS/MWSM"
FTPSettings.UserID = "xxxx"
FTPSettings.Password = "xxxxx"
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
Dim outputStream As New FileStream(filePath + "\" + fileName, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri("ftp://" + FTPSettings.IP + "/" + fileName)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential(FTPSettings.UserID, FTPSettings.Password)
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
MsgBox(cl.ToString) ''at this point
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close()
outputStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End Sub
Public NotInheritable Class FTPSettings
Private Sub New()
End Sub
Public Shared Property IP() As String
Get
Return m_IP
End Get
Set(ByVal value As String)
m_IP = Value
End Set
End Property
Private Shared m_IP As String
Public Shared Property UserID() As String
Get
Return m_UserID
End Get
Set(ByVal value As String)
m_UserID = Value
End Set
End Property
Private Shared m_UserID As String
Public Shared Property Password() As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = Value
End Set
End Property
Private Shared m_Password As String
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Download("C:\", "bank receipt.png")
End Sub
推荐答案
查看以下内容,它将帮助您解决问题
.Net远程处理
Check out the Following like it will help you to Solve problem
.Net Remoting
这篇关于在FTP客户端中获取远程文件大小时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文