- A+
文末附源码文件下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class FtpServerInfo { //FTP登录用户名 public string UserName { get; set; } //FTP登录密码 public string Password { get; set; } //FTP地址(例ftp://127.0.0.1) public string ServerUrl { get; set; } public FtpServerInfo(string serverUrl, string username, string password) { ServerUrl = serverUrl; UserName = username; Password = password; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | public class FtpServer { public static FtpServerInfo ServerInfo { get; set; } public FtpServer(FtpServerInfo _ServerInfo) { ServerInfo = _ServerInfo; } public FtpServer() { } /// <summary> /// 上传 /// </summary> /// <param name="fldPath">fldpath值</param> /// <param name="serverFile">保存相对路径</param> /// <param name="message">返回信息</param> /// <returns></returns> public static bool UploadFile(FileUpload fldPath, string serverFile, out string message)//string localFile { bool result = true; message = ""; try { string path = serverFile.Substring(0, serverFile.LastIndexOf("/")); string[] paths = path.Split('/'); string cPath = ""; foreach (string p in paths) { cPath += p + "/"; CreateDirectory(cPath); } //FileInfo localFileInfo = new FileInfo(localFile); string ftpFileUrl = ServerInfo.ServerUrl + serverFile + Path.GetFileName(fldPath.PostedFile.FileName); FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpFileUrl);//在ftp下创建跟上传文件一样名字的文件 request.KeepAlive = false; request.UseBinary = true; request.ContentLength = fldPath.PostedFile.ContentLength; //request.UsePassive = true; ICredentials credential = new NetworkCredential(ServerInfo.UserName, ServerInfo.Password); request.Credentials = credential; request.Method = WebRequestMethods.Ftp.UploadFile; Stream localFileStream = fldPath.PostedFile.InputStream;//HttpPostFile Stream ftpFileStream = request.GetRequestStream(); try { int BufferSize = 256 * 1024; Byte[] buffer = new Byte[BufferSize]; while (true) { int byteCount = localFileStream.Read(buffer, 0, BufferSize); ftpFileStream.Write(buffer, 0, byteCount); if (byteCount == 0) { break; } } } catch (ThreadAbortException ex) { message = ex.Message; //ftpFileStream.Close(); //localFileStream.Close(); //request.Abort(); result = false; } ftpFileStream.Close(); localFileStream.Close(); //FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //response.Close(); } catch (WebException exc) { message = exc.Message; result = false; } catch (ArgumentException exc) { message = exc.Message; result = false; } return result; } /// <summary> /// 创建文件夹 /// </summary> /// <param name="serverPath">文件夹存放的相对路径</param> /// <returns></returns> public static bool CreateDirectory(string serverPath) { try { FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ServerInfo.ServerUrl + serverPath); ICredentials credential = new NetworkCredential(ServerInfo.UserName, ServerInfo.Password); request.Credentials = credential; request.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); response.Close(); } catch (Exception ex) { //LogHelper.WriteErrLog(ex); return false; } return true; } /// <summary> /// 删除文件夹 /// </summary> /// <param name="serverPath">文件夹存放的相对路径</param> /// <returns></returns> public static bool RemoveDirectory(string serverPath) { bool result = true; try { string ftpFileUrl = ServerInfo.ServerUrl + serverPath; FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpFileUrl); ICredentials credential = new NetworkCredential(ServerInfo.UserName, ServerInfo.Password); request.Credentials = credential; request.Method = WebRequestMethods.Ftp.RemoveDirectory; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); result = response.StatusCode == FtpStatusCode.FileActionOK; response.Close(); } catch (Exception ex) { //LogHelper.WriteErrLog(ex); return false; } return result; } /// <summary> /// 删除文件 /// </summary> /// <param name="serverPath">文件存放的相对路径</param> /// <returns></returns> public static bool RemoveFile(string serverPath) { bool result = true; try { string ftpFileUrl = ServerInfo.ServerUrl + serverPath; FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpFileUrl); ICredentials credential = new NetworkCredential(ServerInfo.UserName, ServerInfo.Password); request.Credentials = credential; request.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); result = response.StatusCode == FtpStatusCode.FileActionOK; response.Close(); } catch (Exception ex) { //LogHelper.WriteErrLog(ex); return false; } return result; } } |
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫