设为首页 收藏本站
查看: 762|回复: 0

Ftp实用类

[复制链接]

尚未签到

发表于 2015-5-27 11:26:26 | 显示全部楼层 |阅读模式
1 public class FtpLib
  2     {
  3         string ftpServerIP;
  4         string ftpUserID;
  5         string ftpPassword;
  6         FtpWebRequest reqFTP;
  7
  8         public FtpLib(string ftpServerIP, string ftpUserID, string ftpPassword)
  9         {
10             this.ftpServerIP = ftpServerIP;
11             this.ftpUserID = ftpUserID;
12             this.ftpPassword = ftpPassword;
13         }
14
15         ///
16         /// 创建连接
17         ///
18         ///
19         private void Connect(String path)
20         {
21             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
22             reqFTP.UseBinary = true;
23             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
24         }
25
26         ///
27         /// 获取文件列表
28         ///
29         ///
30         ///
31         ///
32         private string[] GetFileList(string path, string WRMethods)
33         {
34             string[] downloadFiles;
35             StringBuilder result = new StringBuilder();
36             try
37             {
38                 Connect(path);
39                 reqFTP.Method = WRMethods;
40                 WebResponse response = reqFTP.GetResponse();
41                 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
42                 string line = reader.ReadLine();
43                 while (line != null)
44                 {
45                     result.Append(line);
46                     result.Append("\n");
47                     line = reader.ReadLine();
48                 }
49                 // to remove the trailing '\n'
50                 result.Remove(result.ToString().LastIndexOf('\n'), 1);
51                 reader.Close();
52                 response.Close();
53                 return result.ToString().Split('\n');
54             }
55             catch (Exception ex)
56             {
57                 Helper.WriteLog(ex.Message + "\r\n" + ex.StackTrace);
58                 downloadFiles = null;
59                 return downloadFiles;
60             }
61         }
62
63         ///
64         /// 上传文件
65         ///
66         ///
67         public void Upload(string filename)
68         {
69             FileInfo fileInf = new FileInfo(filename);
70             string uri = "ftp://" + ftpServerIP + "/" +DateTime.Now.ToString("yyyyMMdd")+ "/" + fileInf.Name;
71             Connect(uri);
72             reqFTP.KeepAlive = false;
73             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
74             reqFTP.ContentLength = fileInf.Length;
75             int buffLength = 2048;
76             byte[] buff = new byte[buffLength];
77             int contentLen;
78             FileStream fs = fileInf.OpenRead();
79             try
80             {
81                 Stream strm = reqFTP.GetRequestStream();
82                 contentLen = fs.Read(buff, 0, buffLength);
83                 while (contentLen != 0)
84                 {
85                     strm.Write(buff, 0, contentLen);
86                     contentLen = fs.Read(buff, 0, buffLength);
87                 }
88                 strm.Close();
89                 fs.Close();
90             }
91             catch (Exception ex)
92             {
93                 Helper.WriteLog(ex.Message + "\r\n" + ex.StackTrace);
94             }
95         }
96
97         ///
98         /// 删除文件
99         ///
100         ///
101         public void DeleteFileName(string fileName)
102         {
103             try
104             {
105                 FileInfo fileInf = new FileInfo(fileName);
106                 string uri = "ftp://" + ftpServerIP + "/" + DateTime.Now.ToString("yyyyMMdd") + "/" + fileInf.Name;
107                 Connect(uri);
108                 reqFTP.KeepAlive = false;
109                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
110                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
111                 response.Close();
112             }
113             catch (Exception ex)
114             {
115                 Helper.WriteLog(ex.Message + "\r\n" + ex.StackTrace);
116             }
117         }
118
119         ///
120         /// 判断文件是否存在
121         ///
122         ///
123         ///
124         ///
125         public bool ExecFile(string fileName, string ftpPath)
126         {
127
128             FtpCheckDirectoryExist(ftpPath);
129
130             string[] strList = GetFileList(ftpPath, WebRequestMethods.Ftp.ListDirectoryDetails);
131             if (strList != null)
132             {
133                 if (strList.Length > 0)
134                 {
135                     foreach (string str in strList)
136                     {
137                         if (str.Contains(fileName))
138                         {
139                             return true;
140                         }
141                     }
142                 }
143             }
144             else
145             {
146
147             }
148             return false;
149         }
150
151         //判断文件夹是否存,不存则创建,存在则返回
152         public void FtpCheckDirectoryExist(string destFilePath)
153         {
154             string fullDir = FtpParseDirectory(destFilePath);
155             string[] dirs = fullDir.Split('/');
156             string curDir = DateTime.Now.ToString("yyyyMMdd");
157             for (int i = 0; i < dirs.Length; i++)
158             {
159                 string dir = dirs;
160                 if (dir == string.Empty)
161                 {
162                     try
163                     {
164                         curDir += dir + "/";
165                         FtpMakeDir(curDir);
166                         break;
167                     }
168                     catch (Exception)
169                     { }
170                 }
171             }
172         }
173         public string FtpParseDirectory(string destFilePath)
174         {
175             return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
176         }
177         //创建目录  
178         public Boolean FtpMakeDir(string localFile)
179         {
180             FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + this.ftpServerIP + "/" + localFile);
181             req.Credentials = new NetworkCredential(this.ftpUserID, this.ftpPassword);
182             req.Method = WebRequestMethods.Ftp.MakeDirectory;
183             try
184             {
185                 FtpWebResponse response = (FtpWebResponse)req.GetResponse();
186                 response.Close();
187             }
188             catch (Exception)
189             {
190                 req.Abort();
191                 return false;
192             }
193             req.Abort();
194             return true;
195         }
196     }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-71201-1-1.html 上篇帖子: 使用WindowsIIS架设FTP 下篇帖子: Ftp 类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表