FtpWebRequest.docx
《FtpWebRequest.docx》由会员分享,可在线阅读,更多相关《FtpWebRequest.docx(14页珍藏版)》请在冰豆网上搜索。
![FtpWebRequest.docx](https://file1.bdocx.com/fileroot1/2022-12/30/175879c2-b161-467f-85a0-7df1797748a5/175879c2-b161-467f-85a0-7df1797748a51.gif)
FtpWebRequest
//FtpWebRequest实现FTP上传
//FtpWebRequest实现FTP上传
//FtpWebRequest实现FTP上传
//FtpWebRequest实现FTP上传
//FtpWebRequest实现FTP上传
//FtpWebRequest实现FTP上传
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="完成";
returntrue;
}
catch(Exceptionex)
{
errorinfo=string.Format("因{0},无法完成上传",ex.Message);
returnfalse;
}
}
#endregion
#region下载文件
///
///下载文件
///
///
///
///
///
publicboolDownload(stringftpfilepath,stringfilePath,stringfileName,outstringerrorinfo)////上面的代码实现了
从ftp服务器下载文件的功能
{
try
{
filePath=filePath.Replace("我的电脑\\","");
StringonlyFileName=Path.GetFileName(fileName);
stringnewFileName=filePath+onlyFileName;
if(File.Exists(newFileName))
{
errorinfo=string.Format("本地文件{0}已存在,无法下载",newFileName);
returnfalse;
}
ftpfilepath=ftpfilepath.Replace("\\","/");
stringurl="ftp:
//"+ftpfilepath;
Connect(url);//连接
reqFTP.Credentials=newNetworkCredential(ftpUserID,ftpPassword);
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);
readCount=ftpStream.Read(buffer,0,bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
errorinfo="";
returntrue;
}
catch(Exceptionex)
{
errorinfo=string.Format("因{0},无法下载",ex.Message);
returnfalse;
}
}
#endregion
#region删除文件
///
///删除文件
///
///
publicvoidDeleteFileName(stringfileName)
{
try
{
FileInfofileInf=newFileInfo(fileName);
stringuri="ftp:
//"+ftpServerIP+"/"+fileInf.Name;
Connect(uri);//连接
//默认为true,连接不会被关闭
//在一个命令之后被执行
reqFTP.KeepAlive=false;
//指定执行什么命令
reqFTP.Method=WebRequestMethods.Ftp.DeleteFile;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message,"删除错误");
}
}
#endregion
#region在ftp上创建目录
///
///在ftp上创建目录
///
///
publicvoidMakeDir(stringdirName)
{
try
{
stringuri="ftp:
//"+ftpServerIP+"/"+dirName;
Connect(uri);//连接
reqFTP.Method=WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message);
}
}
#endregion
#region删除ftp上目录
///
///删除ftp上目录
///
///
publicvoiddelDir(stringdirName)
{
try
{
stringuri="ftp:
//"+ftpServerIP+"/"+dirName;
Connect(uri);//连接
reqFTP.Method=WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message);
}
}
#endregion
#region获得ftp上文件大小
///
///获得ftp上文件大小
///
///
///
publiclongGetFileSize(stringfilename)
{
longfileSize=0;
filename=filename.Replace("\\","/");
try
{
//FileInfofileInf=newFileInfo(filename);
//stringuri1="ftp:
//"+ftpServerIP+"/"+fileInf.Name;
//stringuri=filename;
stringuri="ftp:
//"+filename;
Connect(uri);//连接
reqFTP.Method=WebRequestMethods.Ftp.GetFileSize;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
fileSize=response.ContentLength;
response.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message);
}
returnfileSize;
}
#endregion
#regionftp上文件改名
///
///ftp上文件改名
///
///
///
publicvoidRename(stringcurrentFilename,stringnewFilename)
{
try
{
FileInfofileInf=newFileInfo(currentFilename);
stringuri="ftp:
//"+ftpServerIP+"/"+fileInf.Name;
Connect(uri);//连接
reqFTP.Method=WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo=newFilename;
FtpWebResponseresponse=(FtpWebResponse)reqFTP.GetResponse();
//StreamftpStream=response.GetResponseStream();
//ftpStream.Close();
response.Close();
}
catch(Exceptionex)
{
//MessageBox.Show(ex.Message);
}
}
#endregion
#region获得文件明晰
///
///获得文件明晰
///
///
publicstring[]GetFilesDetailList()
{
returnGetFileList("ftp:
//"+ftpServerIP+"/",WebRequestMethods.Ftp.ListDirectoryDetails);
}
///
///获得文件明晰
///
///