如何创建可恢复的文件上传器控制台应用程序?
我正在将数千个文件上传到服务器。
服务器连接中断分配,所以我需要一种方法让这个控制台应用程序能够在连接失败几秒钟等情况下恢复。
我的应用程序很简单,它只是获取 c:\uploads 文件夹中的所有文件,然后使用 Web 服务将文件上传到服务器。
所以:
foreach(文件中的字符串文件) { 上传到服务器(文件); 我
怎样才能做到这一点,以便在连接失败时能够重新恢复? (失败通常只持续几秒钟)
I am uploading thousands of files to a server.
The server connection breaks allot, so I need a way for this console application to be able to recover if the connection fails for a few seconds etc.
My application is simple, it just gets all the files in the c:\uploads folder and then uses a web service to upload the files to the server.
so:
foreach(string file in files)
{
UploadToServer(file);
}
How can I make this so it re-covers in the event of a connection failure? (failures usually last just a few seconds)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用一个小辅助方法,在认输之前重试上传几次。例如:
根据需要调整异常处理代码。
Use a little helper method that retries the upload several times before throwing in the towel. For example:
Tweak the exception handling code as needed.
如果文件上传失败,是否会抛出异常?如果有,则处理异常,并将这些文件存储在某种容器中以便稍后重试,或者您可以放置某种 Thread.Sleep 稍等一下然后重试。
If the files fail to upload, is there an exception that's thrown? If there is, then handle the exception, and either store those files in some kind of container for retrying later, or maybe you can put some kind of Thread.Sleep to wait a little and try again.