博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Ftp实用类
阅读量:6463 次
发布时间:2019-06-23

本文共 6813 字,大约阅读时间需要 22 分钟。

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 try104 {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 else145 {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[i];160 if (dir == string.Empty)161 {162 try163 {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 try184 {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 }

 

转载于:https://www.cnblogs.com/jeffqing/p/3236755.html

你可能感兴趣的文章
AJAX POST&跨域 解决方案 - CORS
查看>>
开篇,博客的申请理由
查看>>
Servlet 技术全总结 (已完成,不定期增加内容)
查看>>
[JSOI2008]星球大战starwar BZOJ1015
查看>>
centos 7 部署LDAP服务
查看>>
揭秘马云帝国内幕:马云的野心有多大
查看>>
iOS项目分层
查看>>
一个action读取另一个action里的session
查看>>
IntelliJ IDEA 注册码
查看>>
String字符串的截取
查看>>
DynamoDB Local for Desktop Development
查看>>
用javascript验证哥德巴赫猜想
查看>>
Shell编程-环境变量配置文件
查看>>
[Unity3d]DrawCall优化手记
查看>>
Struts2和Spring MVC的区别
查看>>
理解Javascript参数中的arguments对象
查看>>
p2:千行代码入门python
查看>>
bzoj1106[POI2007]立方体大作战tet*
查看>>
spring boot configuration annotation processor not found in classpath问题解决
查看>>
团队项目测试报告与用户反馈
查看>>