Ftp上传大文件导致VB.Net应用程序挂起Stream.Close()



我试图将一个大文件(超过50Mb)上传到我的Web服务器,但我的应用程序在试图关闭流时挂起。如果上传的文件大于50Mb,那么.Close()会导致它挂起——根本没有错误消息——但是小于50Mb的文件会成功。

你有什么建议绕过这条小溪。Close()挂起我的应用程序?

Dim target As New Uri(uploadedFilePath)
Dim fRequest As System.Net.FtpWebRequest = System.Net.WebRequest.Create(target)
fRequest.Credentials = New System.Net.NetworkCredential(usr, pswd)
fRequest.KeepAlive = False
fRequest.Proxy = Nothing
fRequest.UsePassive = True
fRequest.UseBinary = True
fRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
fRequest.Timeout = 180000
Dim count As Integer = 0
Dim readBytes As Integer = 0
Const bufferLength As Integer = 8192
Dim buffer As Byte() = New Byte(bufferLength - 1) {}
Dim fs As FileStream = File.OpenRead(localFileName)
Dim fStream As Stream = fRequest.GetRequestStream
Console.WriteLine("Writing bytes to the stream. {0}", String.Format("{0:HH:mm}", Now))
Do
    readBytes = fs.Read(buffer, 0, bufferLength)
    fStream.Write(buffer, 0, readBytes)
    count += readBytes
Loop While readBytes <> 0
Console.WriteLine("Writing {0} bytes to the stream. {1}", count, String.Format("{0:HH:mm}", Now))
fStream.Close()
Console.WriteLine("fstream Closed {0}", String.Format("{0:HH:mm}", Now))

其输出为:

Writing bytes to the stream. 13:08
Writing 51391500 bytes to the stream. 13:18

请注意最后一个Console.Writeline从不输出。

PS使用Visual Studio 2010和.Net Framework 4.0

我注意到您没有关闭输入文件流(fs)。我有一个流程,FTP一个文件,然后将其移动到一个"成功"文件夹。因为fs没有关闭,所以当我试图移动它时,我得到了一个"另一个进程正在使用的文件"。我在Try捕获中捕获了它。我不确定是不是这样,因为你说50MB文件成功了,但这只是一个想法。

我将放入fs.close(),看看它是否能解决我的错误。我移动了一个189MB的文件,这花了我使用的连接的一些时间。。。但它似乎一直有效,直到搬家。

那么,文件本身到达FTP了吗?如果是这样的话,一个变通办法就是在这个特定的时刻中断流程,然后让它顺其自然

你试过摆弄设置吗?

我认为您可能只需要增加超时时间。

把一个Try-Catch放在循环外捕获返回的错误,你可能会发现错误是:

基础连接已关闭

希望能有所帮助!

最新更新