代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C#
】
ftp操作类,ftp上传,下载,创建目录,检查文件存在,
作者:
天磊
/ 发布于
2016/6/29
/
852
ftp操作类,ftp上传,下载,创建目录,检查文件存在,删除文件
using System; using System.IO; using System.Net; using System.Text; using System.Diagnostics; using System.Text.RegularExpressions; using System.Web; namespace IS.Common { /// <summary> /// ftp操作类,ftp上传,下载,创建目录,检查文件存在,删除文件 /// </summary> public class Ftp { /// <summary> /// 从ftp上下载文件 /// </summary> /// <param name="filePath">下载到本地的文件路径</param> /// <param name="FileSrc">ftp源文件夹</param> /// <param name="fileName">下载后的文件名</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> public static void Download(string filePath, string FileSrc, string fileName, string ftpServerIP, string ftpUserName, string ftpPwd) { if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } using (FileStream OutputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create)) { FtpWebRequest ReqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FileSrc)); ReqFTP.Method = WebRequestMethods.Ftp.DownloadFile; ReqFTP.UseBinary = true; ReqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); using (FtpWebResponse response = (FtpWebResponse)ReqFTP.GetResponse()) { using (Stream FtpStream = response.GetResponseStream()) { long Cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = FtpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { OutputStream.Write(buffer, 0, readCount); readCount = FtpStream.Read(buffer, 0, bufferSize); } FtpStream.Close(); } response.Close(); } OutputStream.Close(); } } /// <summary> /// 从服务器上传文件到FTP上 /// </summary> /// <param name="sFileDstPath">源文件夹名称</param> /// <param name="FolderName">ftp服务器的目标文件夹</param> /// <param name="ftpServerIP">ftp ip</param> /// <param name="ftpUserName">用户名</param> /// <param name="ftpPwd">密码</param> public static void UploadSmall(string sFileDstPath, string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { FileInfo fileInf = new FileInfo(sFileDstPath); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/" + fileInf.Name)); reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInf.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; using (FileStream fs = fileInf.OpenRead()) { using (Stream strm = reqFTP.GetRequestStream()) { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); } fs.Close(); } } /// <summary> /// 删除FTP上的文件 /// </summary> /// <param name="IName">要删除的文件数组</param> /// <param name="FolderName">要删除文件所在的ftp文件夹</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> public static void DeleteFtpFile(string[] IName, string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { foreach (string ImageName in IName) { string[] FileList = GetFileList(FolderName, ftpServerIP, ftpUserName, ftpPwd); for (int i = 0; i < FileList.Length; i++) { string Name = FileList[i].ToString(); if (Name == ImageName) { FtpWebRequest ReqFTP; ReqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/" + ImageName)); ReqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); ReqFTP.KeepAlive = false; ReqFTP.Method = WebRequestMethods.Ftp.DeleteFile; ReqFTP.UseBinary = true; using (FtpWebResponse Response = (FtpWebResponse)ReqFTP.GetResponse()) { long size = Response.ContentLength; using (Stream datastream = Response.GetResponseStream()) { using (StreamReader sr = new StreamReader(datastream)) { sr.ReadToEnd(); sr.Close(); } datastream.Close(); } Response.Close(); } } } } } /// <summary> /// 检查文件是否存在 /// </summary> /// <param name="FolderName">ftp文件夹</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> /// <returns></returns> public static string[] GetFileList(string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { string[] downloadFiles; StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/")); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } // to remove the trailing '\n' result.Remove(result.ToString().LastIndexOf('\n'), 1); reader.Close(); response.Close(); return result.ToString().Split('\n'); } catch (Exception ex) { downloadFiles = null; return downloadFiles; } } /// <summary> /// 从客户端上传文件到FTP上 /// </summary> /// <param name="sFilePath">客户端文件上传类</param> /// <param name="filename">ftp文件名</param> /// <param name="FolderName">ftp文件夹</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> public static void UploadFtp(HttpPostedFile sFilePath, string filename, string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { //获取的服务器路径 //FileInfo fileInf = new FileInfo(sFilePath); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName + "/" + filename)); reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = sFilePath.ContentLength; //设置缓存 int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; using (Stream fs = sFilePath.InputStream) { using (Stream strm = reqFTP.GetRequestStream()) { contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); } fs.Close(); } } /// <summary> /// 创建目录 /// </summary> /// <param name="FolderName">ftp目录</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> public static void CreateDirectory(string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { //创建日期目录 try { FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + FolderName)); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); } catch { } } private static Regex regexName = new Regex(@"[^\s]*$", RegexOptions.Compiled); /// <summary> /// 检查日期目录和文件是否存在 /// </summary> /// <param name="FolderName">文件(夹)名</param> /// <param name="ftpServerIP"></param> /// <param name="ftpUserName"></param> /// <param name="ftpPwd"></param> /// <returns></returns> public static bool CheckFileOrPath(string FolderName, string ftpServerIP, string ftpUserName, string ftpPwd) { //检查一下日期目录是否存在 FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPwd); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails; Stream stream = reqFTP.GetResponse().GetResponseStream(); using (StreamReader sr = new StreamReader(stream)) { string line = sr.ReadLine(); while (!string.IsNullOrEmpty(line)) { GroupCollection gc = regexName.Match(line).Groups; if (gc.Count != 1) { throw new ApplicationException("FTP 返回的字串格式不正确"); } string path = gc[0].Value; if (path == FolderName) { return true; } line = sr.ReadLine(); } } return false; } } }
试试其它关键字
同语言下
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
可能有用的
.
C#实现的html内容截取
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
.
实现对图片上传的接收
.
去除字符串中的空格,回车,换行符转变成‘;’在按‘
.
按照回车换行符分割字符串
.
文件MD5码 比较,检测文件是否一样
天磊
贡献的其它代码
(
13
)
.
图片工具类完成图片的截取和任意缩放
.
队列的顺序存储实现和栈的链式实现
.
纯DIV+CSS百分比进度条
.
微信扫码支付接口开发支付宝即时到帐开发
.
ftp操作类,ftp上传,下载,创建目录,检查文件存在,
.
DataTable添加行列
.
jquery自适应浏览器高度,兼容性好
.
设置网页刷新或者重新加载后滚动条的位置不变
.
获取操作系统是32位或64位的代码
.
Css背景颜色透明(#ddd)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3