c#实现FTP方法一FtpWebRequest.docx

上传人:b****2 文档编号:2314897 上传时间:2022-10-28 格式:DOCX 页数:14 大小:16.84KB
下载 相关 举报
c#实现FTP方法一FtpWebRequest.docx_第1页
第1页 / 共14页
c#实现FTP方法一FtpWebRequest.docx_第2页
第2页 / 共14页
c#实现FTP方法一FtpWebRequest.docx_第3页
第3页 / 共14页
c#实现FTP方法一FtpWebRequest.docx_第4页
第4页 / 共14页
c#实现FTP方法一FtpWebRequest.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

c#实现FTP方法一FtpWebRequest.docx

《c#实现FTP方法一FtpWebRequest.docx》由会员分享,可在线阅读,更多相关《c#实现FTP方法一FtpWebRequest.docx(14页珍藏版)》请在冰豆网上搜索。

c#实现FTP方法一FtpWebRequest.docx

c#实现FTP方法一FtpWebRequest

classFTP_Class

{

stringftpServerIP;

stringftpUserID;

stringftpPassword;

FtpWebRequestreqFTP;

publicvoidConnecttest(stringftpServerIP,stringftpUserID,stringftpPassword)

{

//根据uri创建FtpWebRequest对象

reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri("ftp:

//"+ftpServerIP));

//指定数据传输类型

reqFTP.UseBinary=true;

//ftp用户名和密码

reqFTP.Credentials=newNetworkCredential(ftpUserID,ftpPassword);

}

#region连接

///

///连接

///

///

privatevoidConnect(Stringpath)//连接ftp

{

//根据uri创建FtpWebRequest对象

reqFTP=(FtpWebRequest)FtpWebRequest.Create(newUri(path));

//指定数据传输类型

reqFTP.UseBinary=true;

//ftp用户名和密码

reqFTP.Credentials=newNetworkCredential(ftpUserID,ftpPassword);

}

#endregion

#regionftp登录信息

///

///ftp登录信息

///

///ftpServerIP

///ftpUserID

///ftpPassword

publicvoidFtpUpDown(stringftpServerIP,stringftpUserID,stringftpPassword)

{

this.ftpServerIP=ftpServerIP;

this.ftpUserID=ftpUserID;

this.ftpPassword=ftpPassword;

}

#endregion

#region获取文件列表

///

///获取文件列表

///

///

///

///

privatestring[]GetFileList(stringpath,stringWRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表

{

string[]downloadFiles;

StringBuilderresult=newStringBuilder();

try

{

Connect(path);

reqFTP.Method=WRMethods;

WebResponseresponse=reqFTP.GetResponse();

StreamReaderreader=newStreamReader(response.GetResponseStream(),System.Text.Encoding.UTF8);//中文文件名

stringline=reader.ReadLine();

while(line!

=null)

{

result.Append(line);

result.Append("\n");

line=reader.ReadLine();

}

//toremovethetrailing'\n'

result.Remove(result.ToString().LastIndexOf('\n'),1);

reader.Close();

response.Close();

returnresult.ToString().Split('\n');

}

catch(Exceptionex)

{

System.Windows.Forms.MessageBox.Show(ex.Message);

downloadFiles=null;

returndownloadFiles;

}

}

publicstring[]GetFileList(stringpath)//上面的代码示例了如何从ftp服务器上获得文件列表

{

returnGetFileList("ftp:

//"+ftpServerIP+"/"+path,WebRequestMethods.Ftp.ListDirectory);

}

publicstring[]GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表

{

returnGetFileList("ftp:

//"+ftpServerIP+"/",WebRequestMethods.Ftp.ListDirectory);

}

#endregion

#region上传文件

///

///上传文件

///

///

publicboolUpload(stringfilename,stringpath,outstringerrorinfo)//上面的代码实现了从ftp服务器上载文件的功能

{

path=path.Replace("\\","/");

FileInfofileInf=newFileInfo(filename);

stringuri="ftp:

//"+path+"/"+fileInf.Name;

Connect(uri);//连接

//默认为true,连接不会被关闭

//在一个命令之后被执行

reqFTP.KeepAlive=false;

//指定执行什么命令

reqFTP.Method=WebRequestMethods.Ftp.UploadFile;

//上传文件时通知服务器文件的大小

reqFTP.ContentLength=fileInf.Length;

//缓冲大小设置为kb

intbuffLength=2048;

byte[]buff=newbyte[buffLength];

intcontentLen;

//打开一个文件流(System.IO.FileStream)去读上传的文件

FileStreamfs=fileInf.OpenRead();

try

{

//把上传的文件写入流

Streamstrm=reqFTP.GetRequestStream();

//每次读文件流的kb

contentLen=fs.Read(buff,0,buffLength);

//流内容没有结束

while(contentLen!

=0)

{

//把内容从filestream写入uploadstream

strm.Write(buff,0,contentLen);

contentLen=fs.Read(buff,0,buffLength);

}

//关闭两个流

strm.Close();

fs.Close();

errorinfo="完成";

returntrue;

}

catch(Exceptionex)

{

errorinfo=string.Format("因{0},无法完成上传",ex.Message);

returnfalse;

}

}

#endregion

#region续传文件

///

///续传文件

///

///

publicboolUpload(stringfilename,longsize,stringpath,outstringerrorinfo)//上面的代码实现了从ftp服务器上载文件的功能

{

path=path.Replace("\\","/");

FileInfofileInf=newFileInfo(filename);

//stringuri="ftp:

//"+path+"/"+fileInf.Name;

stringuri="ftp:

//"+path;

Connect(uri);//连接

//默认为true,连接不会被关闭

//在一个命令之后被执行

reqFTP.KeepAlive=false;

//指定执行什么命令

reqFTP.Method=WebRequestMethods.Ftp.AppendFile;

//上传文件时通知服务器文件的大小

reqFTP.ContentLength=fileInf.Length;

//缓冲大小设置为kb

intbuffLength=2048;

byte[]buff=newbyte[buffLength];

intcontentLen;

//打开一个文件流(System.IO.FileStream)去读上传的文件

FileStreamfs=fileInf.OpenRead();

try

{

StreamReaderdsad=newStreamReader(fs);

fs.Seek(size,SeekOrigin.Begin);

//把上传的文件写入流

Streamstrm=reqFTP.GetRequestStream();

//每次读文件流的kb

contentLen=fs.Read(buff,0,buffLength);

//流内容没有结束

while(contentLen!

=0)

{

//把内容从filestream写入uploadstream

strm.Write(buff,0,contentLen);

contentLen=fs.Read(buff,0,buffLength);

}

//关闭两个流

strm.Close();

fs.Close();

errorinfo="完成";

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 法律文书 > 判决书

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1