C#从ftp下载文件如何判断下载结束
3个回答
展开全部
从流里读取的字节数为0时,则下载结束。
c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
//从ftp服务器上下载文件的功能
public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create);
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 默认为true,连接不会被关闭
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = ftpresponse.GetResponseStream();
long cl = ftpresponse.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
ftpresponse.Close();
}
catch (Exception ex)
{
}
}
//从http服务器上下载文件的功能
//为服务器路径server_path
//本地存储路径local_path
public void Download(string server_path, string local_path)
{
HttpWebRequest req;
try
{
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
// 根据uri创建FtpWebRequest对象
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(server_path));
// 指定执行什么命令
req.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
if (File.Exists(local_path))
{
FileInfo localFile = new FileInfo(local_path);
if (localFile.LastWriteTime > response.LastModified) return;
}
FileStream outputStream = new FileStream(local_path, FileMode.Create);
Stream stream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = stream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = stream.Read(buffer, 0, bufferSize);
}
stream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
//从共享文件夹下载
public void Download(string server_path, string local_path, int i)
{
try
{
server_path = "\\" + server_path;
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
if (!File.Exists(server_path)) return;
//服务器文件信息
FileInfo serverInfo = new FileInfo(server_path);
DateTime serverTime = serverInfo.LastWriteTime;
if (File.Exists(local_path))
{
FileInfo localinfo = new FileInfo(local_path);
DateTime localTime = localinfo.LastWriteTime;
if (localTime >= serverTime)
return;
}
serverInfo.CopyTo(local_path, true);
return;
}
catch (Exception ex)
{
string s = ex.Message;
}
}
c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载
//从ftp服务器上下载文件的功能
public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create);
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
// 指定执行什么命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 默认为true,连接不会被关闭
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = ftpresponse.GetResponseStream();
long cl = ftpresponse.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
ftpresponse.Close();
}
catch (Exception ex)
{
}
}
//从http服务器上下载文件的功能
//为服务器路径server_path
//本地存储路径local_path
public void Download(string server_path, string local_path)
{
HttpWebRequest req;
try
{
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
// 根据uri创建FtpWebRequest对象
req = (HttpWebRequest)HttpWebRequest.Create(new Uri(server_path));
// 指定执行什么命令
req.Method = WebRequestMethods.Http.Get;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
if (File.Exists(local_path))
{
FileInfo localFile = new FileInfo(local_path);
if (localFile.LastWriteTime > response.LastModified) return;
}
FileStream outputStream = new FileStream(local_path, FileMode.Create);
Stream stream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = stream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = stream.Read(buffer, 0, bufferSize);
}
stream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
//从共享文件夹下载
public void Download(string server_path, string local_path, int i)
{
try
{
server_path = "\\" + server_path;
string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
if (!File.Exists(server_path)) return;
//服务器文件信息
FileInfo serverInfo = new FileInfo(server_path);
DateTime serverTime = serverInfo.LastWriteTime;
if (File.Exists(local_path))
{
FileInfo localinfo = new FileInfo(local_path);
DateTime localTime = localinfo.LastWriteTime;
if (localTime >= serverTime)
return;
}
serverInfo.CopyTo(local_path, true);
return;
}
catch (Exception ex)
{
string s = ex.Message;
}
}
推荐于2016-05-06 · 知道合伙人软件行家
关注
展开全部
从流里读取的字节数为0时,则下载结束。
/// <summary>
/// 单个文件下载方法
/// </summary>
/// <param name="adss">保存文件的本地路径</param>
/// <param name="ftpadss">下载文件的FTP路径</param>
public void download(string adss, string ftpadss)
{
//FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
//FileMode.Create如果文件已存在,它将被改写
FileStream outputStream = new FileStream(adss, FileMode.Create);
FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
//设置要发送到 FTP 服务器的命令
downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0) //有数据则读
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize); //本次读到多少,如为0,则没有了。
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
/// <summary>
/// 单个文件下载方法
/// </summary>
/// <param name="adss">保存文件的本地路径</param>
/// <param name="ftpadss">下载文件的FTP路径</param>
public void download(string adss, string ftpadss)
{
//FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
//FileMode.Create如果文件已存在,它将被改写
FileStream outputStream = new FileStream(adss, FileMode.Create);
FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
//设置要发送到 FTP 服务器的命令
downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0) //有数据则读
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize); //本次读到多少,如为0,则没有了。
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
追问
好的谢谢
追答
关键就是在于 readCount
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
从流里读取的字节数为0时,则下载结束。
/// <summary>
/// 单个文件下载方法
/// </summary>
/// <param name="adss">保存文件的本地路径</param>
/// <param name="ftpadss">下载文件的FTP路径</param>
public void download(string adss, string ftpadss)
{
//FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
//FileMode.Create如果文件已存在,它将被改写
FileStream outputStream = new FileStream(adss, FileMode.Create);
FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
//设置要发送到 FTP 服务器的命令
downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0) //有数据则读
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize); //本次读到多少,如为0,则没有了。
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
关键就是在于 readCount
/// <summary>
/// 单个文件下载方法
/// </summary>
/// <param name="adss">保存文件的本地路径</param>
/// <param name="ftpadss">下载文件的FTP路径</param>
public void download(string adss, string ftpadss)
{
//FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
//FileMode.Create如果文件已存在,它将被改写
FileStream outputStream = new FileStream(adss, FileMode.Create);
FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
//设置要发送到 FTP 服务器的命令
downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0) //有数据则读
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize); //本次读到多少,如为0,则没有了。
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
关键就是在于 readCount
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |