VB.NET FTP服务器上下载文件
2008-02-29 18:13
555 查看
一个子程序是有关从FTP服务器上下载文件的。
Sub getFileFromFTP(ByVal localFile As String, ByVal remoteFile As String, ByVal host As String, ByVal username As String, ByVal password As String)
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
'we want a binary transfer, not textual data
ftp.UseBinary = False
'Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
End Sub
Sub getFileFromFTP(ByVal localFile As String, ByVal remoteFile As String, ByVal host As String, ByVal username As String, ByVal password As String)
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(URI), FtpWebRequest)
ftp.Credentials = New System.Net.NetworkCredential(username, password)
ftp.KeepAlive = False
'we want a binary transfer, not textual data
ftp.UseBinary = False
'Define the action required (in this case, download a file)
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
End Sub
相关文章推荐
- VB.NET中使用FTP下载文件的两种方法。
- VB.NET中使用FTP下载文件的两种方法
- VB.NET中使用FTP下载文件的两种方法
- vb.net上传文件到FTP服务器
- VB.NET中使用FTP下载文件的两种方法。
- VB.NET中使用FTP下载文件的两种方法
- 利用API在FTP服务器上上传和下载文件
- JAVA连接FTP服务器,并上传/下载文件代码
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- 批处理bat下载FTP服务器上某个目录下的文件
- asp.net 弹出下载对话框 将文件从服务器下载到本地
- [VB.NET]新手上路,求.net2003中文版安装文件下载?
- VB程序员的FTP编程指南1.4-下载或上传文件
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- java FTP 工具类 同步文件夹 下载文件..等操作(FtpHelper 需要commons-net-3.5.jar)
- 批处理下载FTP服务器上指定分钟数之前的文件
- java做的比较完善的FTP上传下载文件服务器源码
- 下载文件 PDF 问题 vb.net
- VB.Net 使用FTPWebRequest下载进度条
- java 远程从FTP服务器下载文件到本地