C#本地文件下载以及FTP文件服务下载(以Pdf文件为例)
一、c#实现本地文件下载
1、文件下载的路径 文件名称 以及文件下载之后要放的位置 这三个变量是必须要的
2、定义以下四个对象:
filewebrequest ftpwebrequest = null;
filewebresponse ftpwebresponse = null;
stream ftpresponsestream = null;
filestream outputstream = null;
3、创建文件下载存放位置的路径(不需要手动创建,如果路径存在就创建 不存在就不创建)
directory.createdirectory(localfolder);//创建文件夹名称
* 这里提一点 path.combine()这个就是文件路径拼接的函数,会自动判断,在需要的文件加 \\
比如 string filepath= path.combine(“d:”,“test”,"download"); // filepath="d:\\test\download";
4、 然后执行以下代码 即可完成文件下载
ftpwebrequest = (filewebrequest)filewebrequest.create(new uri(uri));
ftpwebrequest.method = webrequestmethods.ftp.downloadfile;
ftpwebresponse = (filewebresponse)ftpwebrequest.getresponse();
ftpresponsestream = ftpwebresponse.getresponsestream();
long contentlength = ftpwebresponse.contentlength;
int buffersize = 2048;
byte[] buffer = new byte[buffersize];
int readcount;
readcount = ftpresponsestream.read(buffer, 0, buffersize);
while (readcount > 0)
{
outputstream.write(buffer, 0, readcount);
readcount = ftpresponsestream.read(buffer, 0, buffersize);
}
5、代码写完之后要思考,下载文件的时候如何出现异常 这时在整个代码加个 try{} catch{}
具体代码如下:
public static void main(string[] args)
{
testfile tf = new testfile();
tf.filedownload("d:/testfile/", "下载ftp文件.txt", "c:/users/17/desktop/文件", "下载ftp文件.txt", datetime.now.toshortdatestring());
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="localpath">本地文件地址(没有文件名)</param>
/// <param name="localfilename">本地文件名</param>
/// <param name="ftppath">下载的ftp的路径</param>
/// <param name="ftpfilename">下载的ftp的文件名</param>
public bool filedownload(string localpath, string localfilename, string ftppath, string ftpfilename, string date)
{
bool success = false;
//ftpwebresponse ftpwebresponse = null;
filewebrequest ftpwebrequest = null;
filewebresponse ftpwebresponse = null;
stream ftpresponsestream = null;
filestream outputstream = null;
try
{
//string date = datetime.now.toshortdatestring().tostring();//获取系统时间
string date1 = date.replace("/", "");//winods 中文件命名不能有 / 去掉指定字符串 /
//localpath = localpath + date1 + "/";//拼接路径
localpath=path.combine(localpath,date1)
directory.createdirectory(localpath);//创建文件夹名称
outputstream = new filestream(localpath + localfilename, filemode.create);//创建文件
string uri = ftprooturl + ftppath + "/" + ftpfilename;//拼接目标文件路径
string uri= path.combine(ftprooturl,ftppath,ftpfilename);
ftpwebrequest = (filewebrequest)filewebrequest.create(new uri(uri));
//ftpwebrequest1.usebinary = true;
ftpwebrequest.method = webrequestmethods.ftp.downloadfile;
ftpwebresponse = (filewebresponse)ftpwebrequest.getresponse();
ftpresponsestream = ftpwebresponse.getresponsestream();
long contentlength = ftpwebresponse.contentlength;
int buffersize = 2048;
byte[] buffer = new byte[buffersize];
int readcount;
readcount = ftpresponsestream.read(buffer, 0, buffersize);
while (readcount > 0)
{
outputstream.write(buffer, 0, readcount);
readcount = ftpresponsestream.read(buffer, 0, buffersize);
}
success = true;
}
catch (exception e)
{
directoryinfo folder = new directoryinfo(localpath);
streamwriter log = new streamwriter(localpath + "/" + datetime.now.toshortdatestring().tostring().replace("/", "") + ".txt", true);
log.writeline("发生异常时间:" + system.datetime.now.toshorttimestring().tostring());
log.writeline("发生异常信息:" + e.message);
log.writeline("发送异常对象:" + e.source);
log.writeline("调用堆栈:" + e.stacktrace.trim());
log.writeline("触动方法:" + e.targetsite);
log.writeline(" " + e.hresult);
log.writeline("数据对象" + e.data);
log.writeline("____________________________________________________________");
log.writeline();
log.close();
success = false;
}
finally
{
if (outputstream != null)
{
outputstream.close();
}
if (ftpresponsestream != null)
{
ftpresponsestream.close();
}
if (ftpwebresponse != null)
{
ftpwebresponse.close();
}
}
return success;
}
二、 ftp 服务文件下载
这个功能其实和本地文件下载一样,只需要加几点即可
1、ftp服务的地址;
具体代码如下
private string ftpip = "**********";
2、ftp文件服务的登录账号以及密码
具体代码如下
private string ftpname = "*********";
private string ftppassword = "******";
private string ftprooturl = string.empty;
ftpwebrequest reqftp;
3、获取ftp服务上的文件名称、ftp文件服务需要下载之后存放的路径以及下载功能的实现
ftpwebrequest ftpwebrequest = null;
ftpwebresponse ftpwebresponse = null;
stream ftpresponsestream = null;
filestream outputstream = null;
try
{
localfilepath = path.combine(localfilepath, ftpfilenametime);
directory.createdirectory(localfilepath);//创建文件夹名称
outputstream = new filestream(path.combine(localfilepath, filename), filemode.create);
string uri = path.combine(ftprooturl, ftpip, filename);
ftpwebrequest = (ftpwebrequest)filewebrequest.create(new uri(uri));
ftpwebrequest.credentials = new networkcredential(ftpname, ftppassword);//登录ftp
ftpwebrequest.usebinary = true;
ftpwebrequest.method = webrequestmethods.ftp.downloadfile;
ftpwebresponse = (ftpwebresponse)ftpwebrequest.getresponse();
ftpresponsestream = ftpwebresponse.getresponsestream();
long contentlength = ftpwebresponse.contentlength;
int buffersize = 2048;
byte[] buffer = new byte[buffersize];
int readcount;
readcount = ftpresponsestream.read(buffer, 0, buffersize);
while (readcount > 0)
{
outputstream.write(buffer, 0, readcount);
readcount = ftpresponsestream.read(buffer, 0, buffersize);
}
}
catch (exception ex)
{
messagebox.show(ex.message)
/*messagebox.show(ex.message + "是否下载日志文件", "发送错误!", messageboxbuttons.okcancel);
//点击确定 就执行下载日志文件,不然就不执行
if (dr == dialogresult.ok)
{
writelog log = new writelog();
log.write_log(ex.message);
}*/
}
finally
{
if (outputstream != null)
{
outputstream.close();
}
if (ftpresponsestream != null)
{
ftpresponsestream.close();
}
if (ftpwebresponse != null)
{
ftpwebresponse.close();
}
}
5、具体代码如下:
namespace ftpdownload
{
public class ftpfiledownload
{
private string ftpip = "*************";
private string ftpname = "2*******";
private string ftppassword = "*********";
private string ftprooturl = string.empty;
ftpwebrequest reqftp;
public static void main(string[] args)
{
ftpfiledownload ftpdown = new ftpfiledownload();
ftpdown.getfilelist("d:\testfile","37.pdt","20190703");
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="localfilepath">下载路径</param>
/// <param name="filename">下载的名称</param>
/// <param name="ftpfilepath">ftp路径</param>
/// <param name="ftpfilenametime">ftp文件的修改时间</param>
public void ftpdownloadfile(string localfilepath, string filename, string ftpfilenametime)
{
ftpwebrequest ftpwebrequest = null;
ftpwebresponse ftpwebresponse = null;
stream ftpresponsestream = null;
filestream outputstream = null;
try
{
localfilepath = path.combine(localfilepath, ftpfilenametime);
directory.createdirectory(localfilepath);//创建文件夹名称
outputstream = new filestream(path.combine(localfilepath, filename), filemode.create);
string uri = path.combine(ftprooturl, ftpip, filename);
ftpwebrequest = (ftpwebrequest)filewebrequest.create(new uri(uri));
ftpwebrequest.credentials = new networkcredential(ftpname, ftppassword);//登录ftp
ftpwebrequest.usebinary = true;
ftpwebrequest.method = webrequestmethods.ftp.downloadfile;
ftpwebresponse = (ftpwebresponse)ftpwebrequest.getresponse();
ftpresponsestream = ftpwebresponse.getresponsestream();
long contentlength = ftpwebresponse.contentlength;
int buffersize = 2048;
byte[] buffer = new byte[buffersize];
int readcount;
readcount = ftpresponsestream.read(buffer, 0, buffersize);
while (readcount > 0)
{
outputstream.write(buffer, 0, readcount);
readcount = ftpresponsestream.read(buffer, 0, buffersize);
}
}
catch (exception ex)
{
messagebox.show(ex.message);
/* dialogresult dr = messagebox.show(ex.message + "是否下载日志文件", "发送错误!", messageboxbuttons.okcancel);
//点击确定 就执行下载日志文件,不然就不执行
if (dr == dialogresult.ok)
{
writelog log = new writelog();
log.write_log(ex.message);
}*/
}
finally
{
if (outputstream != null)
{
outputstream.close();
}
if (ftpresponsestream != null)
{
ftpresponsestream.close();
}
if (ftpwebresponse != null)
{
ftpwebresponse.close();
}
}
}
如有疑问 可以往773408602@qq.com发邮件,我会第一时间给你解答的