我正在尝试通过FTP将文件经常发送到远程计算机。我已成功编写了所有发送/接收代码。现在我想将不同的文件(例如:3个文件)发送到不同的IP(3个Ips)。怎么做?
public bool UploadFile(string file_to_upload,int attempts)
{
if (System.IO.File.Exists(file_to_upload))
{
FtpWebResponse response = null;
FtpWebRequest ftprequest= null;
int Appendinglength = 0;
int TotalLength = 0;
pointer = 1;
long FTPFilesize = 0;
while (--attempts >= 0)
{
try
{
FileInfo finfo = new FileInfo(file_to_upload);
TotalLength = Convert.ToInt32(finfo.Length);
ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + finfo.Name));
ftprequest.Credentials = new NetworkCredential(Uname, Pwd);
ftprequest.UseBinary = true;
ftprequest.Timeout = 500;
ftprequest.KeepAlive = false;
if (ISFtpFileExists(finfo.Name, out FTPFilesize))
{
ftprequest.Method = WebRequestMethods.Ftp.UploadFile;
}
else
{
ftprequest.Method = WebRequestMethods.Ftp.UploadFile;
}
//ftprequest.ContentLength = finfo.Length - FTPFilesize;
ftprequest.UsePassive = false;
fstr = new FileStream(file_to_upload, FileMode.Open);
ToWriteStream = ftprequest.GetRequestStream();
//response = (FtpWebResponse)ftprequest.GetResponse();
attempts = 3000;
//fstr.Seek(FTPFilesize, SeekOrigin.Begin);
byte[] buffer = new byte[bufferLength];
fstr.Read(buffer, 0, bufferLength);
startuptime = DateTime.Now;
Exoccur("Resuming Upload..");
while (pointer != 0)
{
ToWriteStream.Write(buffer, 0, pointer);
Appendinglength += pointer;
int prg = CalculateProgress(Appendinglength, TotalLength);
PrgUpdate(prg);
//lblupdate(prg);
pointer = fstr.Read(buffer, 0, bufferLength);
}
ToWriteStream.Close();
fstr.Close();
break;
}
catch
{
//ToWriteStream.Close();
fstr.Close();
if (attempts == 2999)
{
PrgUpdate(0);
}
Appendinglength = 0;
ftprequest.Abort();
ftprequest = null;
Thread.Sleep(500);
}
finally
{
//attempts = 3000;
}
}
enduptime = DateTime.Now;
TimeSpan timetaken = enduptime.Subtract(startuptime);
double d = timetaken.TotalMilliseconds;
d = d / 1000;
Exoccur(Convert.ToString(d));
}
else
{
Exoccur("File Doesnt occur");
}
return true;
}
以上是FTP中的上传
public bool DownloadFile(string DestPath, string file_to_download)
{
PrgUpdate(0);
FtpWebRequest ftprequest = null;
int TotalLength = 0;
WebResponse response = null;
FileInfo fin = null;
try
{
int Appendinglength = 0;
if (KeepLength != 0)
{
TotalLength = KeepLength;
}
else
{
//FileStream fst = new FileStream(DestPath, FileMode.Create);
TotalLength = Convert.ToInt32(GetFileSize_Remotemachine(file_to_download));
}
fin = new FileInfo(DestPath);
//FtpWebRequest ftprequest;
ftprequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + IP + "/" + file_to_download));
ftprequest.Credentials = new NetworkCredential(Uname, Pwd);
ftprequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftprequest.UseBinary = true;
ftprequest.KeepAlive = false;
if (fin.Exists)
{
ftprequest.ContentOffset = fin.Length;
fst = new FileStream(DestPath, FileMode.Append);
}
else
{
fst = new FileStream(DestPath, FileMode.Create);
}
response = (WebResponse)ftprequest.GetResponse();
Stream GetStream = response.GetResponseStream();
byte[] buffer = new byte[bufferLength];
pointer = GetStream.Read(buffer, 0, bufferLength);
DateTime starttime = DateTime.Now;
while (pointer != 0)
{
fst.Write(buffer, 0, pointer);
Appendinglength += pointer;
PrgUpdate(CalculateProgress(Appendinglength, TotalLength));
pointer = GetStream.Read(buffer, 0, bufferLength);
}
fin = new FileInfo(DestPath);
if ((long)TotalLength == fin.Length)
{
Exoccur("File download completed.");
}
else
{
PrgUpdate(100);
}
DateTime endtime = DateTime.Now;
TimeSpan diff = endtime.Subtract(starttime);
GetStream.Close();
fst.Close();
response.Close();
double d = diff.TotalMilliseconds;
d = d / 1000;
KeepLength = 0;
Exoccur(Convert.ToString(d));
}
catch (Exception ex)
{
ftprequest.Abort();
KeepLength = TotalLength;
PrgUpdate(0);
Exoccur(ex.Message);
return false;
}
finally
{
//response.Close();
fst.Close();
}
return true;
}
这个是下载的
答案 0 :(得分:-3)
我使它工作,我们可以做不同的实现,我使用线程做多个 并发上传。工作