通过FTP方式下载数据时显示进度条的问题
我通过FTP的方式访问FTP中的指定路径,并通过指定路径获取了该路径下的所有文件的列表
方法如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace 获取指定时间段内的数据
{
class FtpUpDown
{
string ftpServerIP; //服务IP
string ftpUserID; //用户名
string ftpPassword; //密码
FtpWebRequest reqFTP; //实现FTP客户端
/// <summary>
///连接ftp
/// </summary>
/// <param name="path"></param>
private void Connect(string path)
{
//根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
reqFTP.UseBinary = true;
//ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
}
/// <summary>
/// 登陆信息
/// </summary>
/// <param name="ftpServerIP"></param>
/// <param name="ftpUserID"></param>
/// <param name="ftpPassword"></param>
public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP;
this.ftpUserID = ftpUserID;
this.ftpPassword = ftpPassword;
}
/// <summary>
/// 从ftp服务器上获得文件列表
/// </summary>
/// <param name="path"></param>
/// <param name="WRMethods"></param>
/// <returns></returns>
public string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表
{
string[] downloadFiles;
StringBuilder result = new StringBuilder(); // StringBuilder是干什么用的? 值为可变字符序列的类似字符串的对象
try
{
Connect("ftp://" + path);
reqFTP.Method = WRMethods;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名
string line = reader.ReadLine(); //有个问题,如何剔除文件夹???
while (line != null)
{
result.Append(line);
result.Append(" ");
line = reader.ReadLine();
}
// to remove the trailing ' '
result.Remove(result.ToString().LastIndexOf(' '), 1);
reader.Close();
response.Close(); //为何要关闭response?
return result.ToString().Split(' ');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public long GetFileSize(string filename)
{
long fileSize = 0;
try
{
FileInfo fileinfo = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileinfo.Name;
Connect(uri); //连接
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
fileSize = response.ContentLength;
response.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return fileSize;
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="filePath"></param>
/// <param name="fileName"></param>
/// <param name="errorinfo"></param>
/// <returns></returns>
//public bool Download(string filePath, string fileName, out string errorinfo)
public bool Download(string filePath, string fileName, out string errorinfo, System.Windows.Forms.ProgressBar prog)
{
try
{
String onlyFileName = Path.GetFileName(fileName);
string newFileName = filePath + "/" + onlyFileName;
if (File.Exists(newFileName))
{
errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
return false;
}
else
{
string url = "ftp://" + ftpServerIP + "/" + fileName;
Connect(url);//连接
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
//if (prog != null) //(新加的)
//{
// prog.Maximum = (int)cl;
//}
long totalDownLoadByte = 0; //(新加的 )
float percent = 0;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
int osize = ftpStream.Read(buffer, 0, (int)buffer.Length); //这句是什么意思?
while (osize > 0) //(新加的)
{
totalDownLoadByte = osize + totalDownLoadByte;
//if (prog != null)
//{
// prog.Value = (int)totalDownLoadByte;
//}
osize = ftpStream.Read(buffer, 0, (int)buffer.Length);
percent = (float)totalDownLoadByte / (float)totalDownLoadByte * 100;
}
readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(newFileName, FileMode.Create);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
errorinfo = "";
return true;
}
}
catch (Exception ex)
{
errorinfo = string.Format("因{0},无法下载", ex.Message);
return false;
}
}
}
}