//上传
ftpmg.Upload("", DateTime.Now.ToString("yyyyMMddhhmmss"));
//下载
ftpmg.Download(@"E:\", "tt.txt", out errorinfo);
1 string ftpServerIP; 2 string ftpUserID; 3 string ftpPassword; 4 FtpWebRequest reqFTP; 5 private void Connect(String path)//连接ftp 6 { 7 // 根据uri创建FtpWebRequest对象 8 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path)); 9 // 指定数据传输类型 10 reqFTP.UseBinary = true; 11 // ftp用户名和密码 12 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 13 } 14 public FtpManager(string ftpServerIP, string ftpUserID, string ftpPassword) 15 { 16 this.ftpServerIP = ftpServerIP; 17 this.ftpUserID = ftpUserID; 18 this.ftpPassword = ftpPassword; 19 20 } 21 22 23 24 25 public void Upload(string path, string filename) //上面的代码实现了向ftp服务器上载文件的功能 26 { 27 FileInfo fileInf = new FileInfo(filename); 28 string uri = ""; 29 if (path != "") 30 uri = "ftp://" + ftpServerIP + "/" + path + "/" + fileInf.Name; 31 else 32 uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 33 34 35 Connect(uri);//连接 36 37 // 默认为true,连接不会被关闭 38 // 在一个命令之后被执行 39 reqFTP.KeepAlive = false; 40 41 // 指定执行什么命令 42 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 43 44 // 上传文件时通知服务器文件的大小 45 reqFTP.ContentLength = fileInf.Length; 46 47 // 缓冲大小设置为kb 48 int buffLength = 2048; 49 50 byte[] buff = new byte[buffLength]; 51 int contentLen; 52 53 // 打开一个文件流(System.IO.FileStream) 去读上传的文件 54 FileStream fs = fileInf.OpenRead(); 55 56 Stream strm = null; 57 try 58 { 59 // 把上传的文件写入流 60 strm = reqFTP.GetRequestStream(); 61 // 每次读文件流的kb 62 contentLen = fs.Read(buff, 0, buffLength); 63 64 // 流内容没有结束 65 while (contentLen != 0) 66 { 67 // 把内容从file stream 写入upload stream 68 strm.Write(buff, 0, contentLen); 69 70 contentLen = fs.Read(buff, 0, buffLength); 71 } 72 73 // 关闭两个流 74 strm.Close(); 75 fs.Close(); 76 } 77 catch (Exception ex) 78 { 79 Log.WriteLogD("上传出错:" + ex.Message); 80 Console.WriteLine(ex.Message); 81 throw ex; 82 } 83 finally 84 { 85 86 fs.Close(); 87 } 88 } 89 90 public bool Download(string filePath, string fileName, out string errorinfo)/**/////上面的代码实现了从ftp服务器下载文件的功能 91 { 92 93 94 try 95 { 96 String onlyFileName = Path.GetFileName(fileName); 97 string newFileName = filePath + "\\" + onlyFileName; 98 if (File.Exists(newFileName)) 99 { 100 errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName); 101 return false; 102 103 } 104 string url = "ftp://" + ftpServerIP + "/" + fileName; 105 Connect(url);//连接 106 107 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 108 109 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 110 111 Stream ftpStream = response.GetResponseStream(); 112 113 long cl = response.ContentLength; 114 115 int bufferSize = 2048; 116 117 int readCount; 118 119 byte[] buffer = new byte[bufferSize]; 120 121 readCount = ftpStream.Read(buffer, 0, bufferSize); 122 FileStream outputStream = new FileStream(newFileName, FileMode.Create); 123 124 while (readCount > 0) 125 { 126 outputStream.Write(buffer, 0, readCount); 127 128 readCount = ftpStream.Read(buffer, 0, bufferSize); 129 } 130 131 ftpStream.Close(); 132 133 outputStream.Close(); 134 135 response.Close(); 136 errorinfo = ""; 137 return true; 138 } 139 catch (Exception ex) 140 { 141 Log.WriteLogD("下载出错:" + ex.Message); 142 errorinfo = string.Format("因{0},无法下载", ex.Message); 143 return false; 144 } 145 } 146