c#实现FTP方法一FtpWebRequestWord文件下载.docx

上传人:b****6 文档编号:16230701 上传时间:2022-11-21 格式:DOCX 页数:14 大小:16.85KB
下载 相关 举报
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

/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);

#region下载文件

///下载文件

filePath"

fileName"

errorinfo"

publicboolDownload(stringftpfilepath,stringfilePath,stringfileName,outstringerrorinfo)////上面的代码实现了从ftp服务器下载文件的功能

filePath=filePath.Replace("

我的电脑\\"

"

StringonlyFileName=Path.GetFileName(fileName);

stringnewFileName=filePath+onlyFileName;

if(File.Exists(newFileName))

本地文件{0}已存在,无法下载"

newFileName);

ftpfilepath=ftpfilepath.Replace("

stringurl="

+ftpfilepath;

Connect(url);

FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();

StreamftpStream=response.GetResponseStream();

longcl=response.ContentLength;

intbufferSize=2048;

intreadCount;

byte[]buffer=newbyte[bufferSize];

readCount=ftpStream.Read(buffer,0,bufferSize);

FileStreamoutputStream=newFileStream(newFileName,FileMode.Create);

while(readCount>

0)

outputStream.Write(buffer,0,readCount);

ftpStream.Close();

outputStream.Close();

因{0},无法下载"

}

#region删除文件

///删除文件

publicvoidDeleteFileName(stringfileName)

FileInfofileInf=newFileInfo(fileName);

reqFTP.Method=WebRequestMethods.Ftp.DeleteFile;

//MessageBox.Show(ex.Message,"

删除错误"

#region在ftp上创建目录

///在ftp上创建目录

dirName"

publicvoidMakeDir(stringdirName)

+dirName;

reqFTP.Method=WebRequestMethods.Ftp.MakeDirectory;

//MessageBox.Show(ex.Message);

#region删除ftp上目录

///删除ftp上目录

publicvoiddelDir(stringdirName)

reqFTP.Method=WebRequestMethods.Ftp.RemoveDirectory;

#region获得ftp上文件大小

///获得ftp上文件大小

publiclongGetFileSize(stringfilename)

longfileSize=0;

filename=filename.Replace("

"

//FileInfofileInf=newFileInfo(filename);

//stringuri1="

//stringuri=filename;

+filename;

reqFTP.Method=WebRequestMethods.Ftp.GetFileSize;

fileSize=response.ContentLength;

returnfileSize;

#regionftp上文件改名

///ftp上文件改名

currentFilename"

newFilename"

publicvoidRename(stringcurrentFilename,stringnewFilename)

FileInfofileInf=newFileInfo(currentFilename);

//连接

reqFTP.Method=WebRequestMethods.Ftp.Rename;

reqFTP.RenameTo=newFilename;

//StreamftpStream=response.GetResponseStream();

//ftpStream.Close();

#region获得文件明晰

///获得文件明晰

publicstring[]GetFilesDetailList()

WebRequestMethods.Ftp.ListDirectoryDetails);

publicstring[]GetFilesDetailList(stringpath)

retur

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

当前位置:首页 > 小学教育 > 小学作文

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

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