VB.NET从FTP服务器下载文件错误 [英] VB.NET Download a File from FTP Server Error

查看:141
本文介绍了VB.NET从FTP服务器下载文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我创建了一个程序,可以从FTP服务器下载文件
它从FTP读取当前日期,如果在下载目录中找不到该日期,则将其下载.当程序下载一个小文件(小于5 MB-这是我在下载目录中看到的文件大小)时,它运行良好,但是当程序下载大于10 MB的文件时,将发生运行时错误.不知道怎么了?

这是错误"The underlying connection was closed: An unexpected error occurred on a receive."

这是下载文件的代码

hey guys, I created a program that downloads a file from FTP Server
It reads the current date from FTP and download it if its not found in download directory. It runs fine when the program downloads a small file(5 MB below - this is the size of a file that I''ve seen in download directory) but when the program downloads a file more than 10 MB a runtime error will occur. Don''t know whats wrong?

This is the Error "The underlying connection was closed: An unexpected error occurred on a receive."

This is the code for downloading a file

Dim request As FtpWebRequest = Nothing
Dim response As FtpWebResponse = Nothing

request = DirectCast(FtpWebRequest.Create(New Uri(ftp)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
request.UseBinary = True
request.Credentials = New NetworkCredential(username, password)
response = DirectCast(request.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()

Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
Dim downloaded As Long = 0
Dim elapseTime As New Stopwatch
elapseTime.Start()
timeStarted = Format(Now, "hh:mm:ss tt")

outputStream = New FileStream(saveLocation + fileName, FileMode.Create)
fileCreated = True
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
    If bckDownloader(index).CancellationPending Then 'If user abort download
        outputStream.Close()
        File.Delete(saveLocation + fileName)
    End If
    downloaded = downloaded + readCount
    outputStream.Write(buffer, 0, readCount)
    readCount = ftpStream.Read(buffer, 0, bufferSize)
    If CInt(downloaded / 1024) - 2 = CInt((fileSize / 1024)) Then
        Dim x As Integer = 0
    End If
    Me.Invoke(safeDownloadStatus, fileName, elapseTime.ElapsedMilliseconds, downloaded, fileSize, 1, index)
End While
Me.Invoke(safeChangeStatusStrip, 1, "")
Me.Invoke(safeLogs, fileName + " has been successfully downloaded")
Me.Invoke(safeDownloadStatus, "", 0, 0, 0, 3, index)

timeEnded = Format(Now, "hh:mm:ss tt")

Dim copyTo As String = downloadPath & dateStr & "\"
If Not Directory.Exists(copyTo) Then Directory.CreateDirectory(copyTo)

Dim logFile As String = copyTo & Format(Now, "hhmmss tt") & Mid(fi.Name, 18, Len(fi.Name))
logFile = Mid(logFile, 1, Len(logFile) - 4) & ".ftl"

WriteFileToLogs(logFile, jobInfo, fileName, dateStr, xlPath, imgCount, fileSize, saveTo & dateStr & "\", dateNow, timeStarted, timeEnded, "File successfully downloaded.", "0")

elapseTime.Stop()
objStreamReader.Close()

推荐答案

以下是您可能要设置的属性列表,以查看它们是否对您有帮助:

Here is a list of properties you may like to set to see if they help you:

request.Timeout = 10000
request.ReadWriteTimeout = 10000
request.KeepAlive = False
request.UseBinary = True



并不是说这些是正确的设置,只是说它们是您应该使用的设置.我认为您的超时时间越长越好. UseBinary应该为true.不确定KeepAlive,我认为这与发送虚拟请求以保持连接打开有关,因此如果您立即在大文件上使用它可能无关紧要.可以尝试一下.



Not saying these are the right settings, just that they are settings you should play with. I think the longer your timeout, the better. UseBinary should be true. Not sure with KeepAlive, I think it has to do with sending dummy requests to keep a connection open, so it probably doesn''t matter if you''re using it right away on a big file. Can''t hurt to try it though.


这篇关于VB.NET从FTP服务器下载文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆