Imports System.Net 'FtpWebRequest
Imports System.IO
Public Class Form1
Public UserName As String
Public UserPassword As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub DownFile(ByVal FTP_FilePath As String, ByVal SavePath As String, ByVal Prog As ProgressBar)
Dim FTP As FtpWebRequest
Try
FTP = FtpWebRequest.Create(New Uri(FTP_FilePath))
FTP.Method = WebRequestMethods.Ftp.DownloadFile '下载 '上传UploadFile
FTP.UseBinary = True
FTP.Credentials = New NetworkCredential(UserName, UserPassword)
Dim Response As FtpWebResponse = FTP.GetResponse
Dim TotalBytes As Long = GetFileSize(FTP_FilePath)
Prog.Maximum = TotalBytes
Dim TotalDownLoadedByte As Long = 0 '当前下载的长度
Dim OutputStream As FileStream = New FileStream(SavePath, FileMode.Create) '输出流
Dim FtpStream As Stream = Response.GetResponseStream
Dim BufferSize As Integer = 1024
Dim Buffer(BufferSize) As Byte
Dim ReadCount As Integer '读取的数量
ReadCount = FtpStream.Read(Buffer, 0, BufferSize) '读取
Do While ReadCount > 0
TotalDownLoadedByte = ReadCount + TotalDownLoadedByte
Prog.Value = TotalDownLoadedByte '设置进度条
OutputStream.Write(Buffer, 0, ReadCount) '写入流
ReadCount = FtpStream.Read(Buffer, 0, BufferSize) '读取
Loop
FtpStream.Close()
OutputStream.Close()
Response.Close()
MsgBox("下载完成")
Catch ex As Exception
MsgBox("下载失败")
End Try
End Sub
'获取文件大小
Private Function GetFileSize(ByVal FTP_FilePath As String) As Long
Dim reqftp As FtpWebRequest
Try
reqftp = FtpWebRequest.Create(New Uri(FTP_FilePath))
reqftp.Method = WebRequestMethods.Ftp.GetFileSize
reqftp.UseBinary = True
reqftp.Credentials = New NetworkCredential(UserName, UserPassword)
Dim response As FtpWebResponse = reqftp.GetResponse
GetFileSize = response.ContentLength '获取文件大小
Catch ex As Exception
GetFileSize = 0
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UserName = "tiancao1001"
UserPassword = "*******"
Dim FTP_FilePath As String = "ftp://www.tiancao.net/wwwroot/ad121.swf"
Dim AppPath As String = Application.StartupPath
DownFile(FTP_FilePath, AppPath & "/1.swf", Me.ProgressBar1)
End Sub
End Class
Imports System.IO
Public Class Form1
Public UserName As String
Public UserPassword As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub DownFile(ByVal FTP_FilePath As String, ByVal SavePath As String, ByVal Prog As ProgressBar)
Dim FTP As FtpWebRequest
Try
FTP = FtpWebRequest.Create(New Uri(FTP_FilePath))
FTP.Method = WebRequestMethods.Ftp.DownloadFile '下载 '上传UploadFile
FTP.UseBinary = True
FTP.Credentials = New NetworkCredential(UserName, UserPassword)
Dim Response As FtpWebResponse = FTP.GetResponse
Dim TotalBytes As Long = GetFileSize(FTP_FilePath)
Prog.Maximum = TotalBytes
Dim TotalDownLoadedByte As Long = 0 '当前下载的长度
Dim OutputStream As FileStream = New FileStream(SavePath, FileMode.Create) '输出流
Dim FtpStream As Stream = Response.GetResponseStream
Dim BufferSize As Integer = 1024
Dim Buffer(BufferSize) As Byte
Dim ReadCount As Integer '读取的数量
ReadCount = FtpStream.Read(Buffer, 0, BufferSize) '读取
Do While ReadCount > 0
TotalDownLoadedByte = ReadCount + TotalDownLoadedByte
Prog.Value = TotalDownLoadedByte '设置进度条
OutputStream.Write(Buffer, 0, ReadCount) '写入流
ReadCount = FtpStream.Read(Buffer, 0, BufferSize) '读取
Loop
FtpStream.Close()
OutputStream.Close()
Response.Close()
MsgBox("下载完成")
Catch ex As Exception
MsgBox("下载失败")
End Try
End Sub
'获取文件大小
Private Function GetFileSize(ByVal FTP_FilePath As String) As Long
Dim reqftp As FtpWebRequest
Try
reqftp = FtpWebRequest.Create(New Uri(FTP_FilePath))
reqftp.Method = WebRequestMethods.Ftp.GetFileSize
reqftp.UseBinary = True
reqftp.Credentials = New NetworkCredential(UserName, UserPassword)
Dim response As FtpWebResponse = reqftp.GetResponse
GetFileSize = response.ContentLength '获取文件大小
Catch ex As Exception
GetFileSize = 0
End Try
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UserName = "tiancao1001"
UserPassword = "*******"
Dim FTP_FilePath As String = "ftp://www.tiancao.net/wwwroot/ad121.swf"
Dim AppPath As String = Application.StartupPath
DownFile(FTP_FilePath, AppPath & "/1.swf", Me.ProgressBar1)
End Sub
End Class
[本日志由 tiancao1001 于 2013-01-09 08:59 PM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |