Ftp 上传大文件导致 VB.Net 应用程序挂在 Stream.Close() 上
我正在尝试将大文件(超过 50Mb)上传到我的网络服务器,但在尝试关闭流时我的应用程序挂起。如果上传的文件大于 50Mb,则 .Close() 会导致其挂起 - 根本没有错误消息 - 但是小于 50Mb 的文件会成功。
您建议如何绕过 fstream.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
I'm trying to upload a large file (over 50Mb) to my webserver but my app hangs when trying to close the stream. If the file uploaded is larger than 50Mb then the .Close() causes it to hang - no error message at all - however a file less than 50Mb succeeds.
What do you suggest to get around the fstream.Close() hanging my app?
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))
which gives the output as:
Writing bytes to the stream. 13:08
Writing 51391500 bytes to the stream. 13:18
Note the last Console.Writeline is never output.
PS Using Visual Studio 2010 and .Net Framework 4.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到您没有关闭输入文件流(fs)。我有一个通过 FTP 传输文件然后将其移动到“成功”文件夹的过程。因为 fs 没有关闭,所以当我尝试移动它时,我收到“文件正在被另一个进程使用”。我用 Try-catch 抓住了它。我不确定是否是这样,因为你说 50MB 文件成功,但这是一个想法。
我将放入
fs.close()
并查看是否可以解决我的错误。我移动了一个 189MB 的文件,这在我使用的连接上花费了一些时间......但在移动之前它似乎一直在工作。I noticed you didn't close the input filestream (fs). I have a process that FTPs a file and then moves it to a 'successful' folder. Because fs wasn't closed, I got a "file being used by another process" when I tried to move it. I caught it in a Try-catch. I'm not sure if that is it, since you say 50MB files succeed, but it's a thought.
I'm going to put in the
fs.close()
and see if that takes care of my error. I moved a 189MB file, which took some time with the connection I was using... but it seemed to work right up until the move.那么,文件本身是否到达了 FTP?如果是这样,解决方法就是在这个特定时刻切断该流程,然后保留它。
您是否尝试过摆弄设置?
So, did the file itself arrive at the FTP? If so, a workaround would be to just cut the process at this given moment, and leave it be.
Have you tried fiddling around with the settings?
我认为你可能只需要增加超时时间。
在循环外部放置一个
Try-Catch
来捕获返回的错误,您可能会发现错误是:希望有帮助!
I think you may just need to increase the timeout.
Put a
Try-Catch
outside the loop to trap the error being returned, you may find the error is:Hope that helps!