FtpWebRequest 多个文件同时上传。求C#代码,举个栗子就好。
多线程执行,此程序会报错:Theoperationhastimedout.StackTrace:atSystem.Net.FtpWebRequest.CheckError...
多线程执行,此程序会报错:The operation has timed out.
StackTrace: at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.GetRequestStream() 展开
StackTrace: at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.GetRequestStream() 展开
2个回答
展开全部
#
using System;
using System.IO;
using System.Net;
namespace Ftp
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo dir = new DirectoryInfo("upload");//上传文件所在的目录
FileInfo[] fileInfos = dir.GetFiles();
foreach (FileInfo item in fileInfos)//遍历目录下的所有文件
{
UploadFile(item);//开始上传
}
Console.WriteLine("done!");
}
private static void UploadFile(FileInfo fi)
{
FileStream fs = fi.OpenRead();
long length = fs.Length;
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://192.168.56.1/" + fi.Name));
req.Credentials = new NetworkCredential("name", "password");
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = true;
req.ContentLength = length;
req.Timeout = 10 * 1000;
try
{
Stream stream = req.GetRequestStream();
int BufferLength = 2048;
byte[] b = new byte[BufferLength];
int i;
while ((i = fs.Read(b, 0, BufferLength)) > 0)
{
stream.Write(b, 0, i);
}
stream.Close();
stream.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
追问
我只能说这是单线程,我现在了解到FTP不能做到几人同时上传文件。所以改变了代码。谢谢你的代码。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询