c#实现FTP方法一FtpWebRequestWord文档格式.docx

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

c#实现FTP方法一FtpWebRequestWord文档格式.docx

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

c#实现FTP方法一FtpWebRequestWord文档格式.docx

//指定数据传输类型

reqFTP.UseBinary=true;

//ftp用户名和密码

reqFTP.Credentials=newNetworkCredential(ftpUserID,ftpPassword);

}

#region连接

///<

summary>

///连接

/summary>

paramname="

path"

>

<

/param>

privatevoidConnect(Stringpath)//连接ftp

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

#endregion

#regionftp登录信息

///ftp登录信息

ftpServerIP"

ftpServerIP<

ftpUserID"

ftpUserID<

ftpPassword"

ftpPassword<

publicvoidFtpUpDown(stringftpServerIP,stringftpUserID,stringftpPassword)

this.ftpServerIP=ftpServerIP;

this.ftpUserID=ftpUserID;

this.ftpPassword=ftpPassword;

#region获取文件列表

///获取文件列表

WRMethods"

returns>

/returns>

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('

),1);

reader.Close();

response.Close();

returnresult.ToString().Split('

catch(Exceptionex)

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

downloadFiles=null;

returndownloadFiles;

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

returnGetFileList("

+ftpServerIP+"

/"

+path,WebRequestMethods.Ftp.ListDirectory);

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

WebRequestMethods.Ftp.ListDirectory);

#endregion

#region上传文件

///上传文件

filename"

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

path=path.Replace("

\\"

"

FileInfofileInf=newFileInfo(filename);

stringuri="

+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();

//把上传的文件写入流

Streamstrm=reqFTP.GetRequestStream();

//每次读文件流的kb

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

//流内容没有结束

while(contentLen!

=0)

//把内容从filestream写入uploadstream

strm.Write(buff,0,contentLen);

//关闭两个流

strm.Close();

fs.Close();

errorinfo="

完成"

;

returntrue;

errorinfo=string.Format("

因{0},无法完成上传"

ex.Message);

returnfalse;

#region续传文件

///续传文件

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

//stringuri="

+path;

//指定执行什么命令

reqFTP.Method=WebRequestMethods.Ftp.AppendFile;

StreamReaderdsad=newStreamReader(fs);

fs.Seek(size,SeekOrigin.Begin);

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

当前位置:首页 > 人文社科 > 法律资料

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

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