使用 FTPWebrequest 类同时上传/下载多个文件
我经常尝试通过 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;
}
,这个是下载的
I'm trying to send files via FTP very often to remote machines. I have succesfully written all the code for sending/receiving. Now i want to send different files (ex:3 files) to different IPs (3 Ips). How to do it?
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;
}
the above one is for Upload in 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;
}
and this one is for download
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它工作了,我们可以做不同的实现,我使用线程来做多个
同时上传。它可以工作
i made it work ,we can do different implementations i used threads for doing multiple
uploads concurrently.it works