public byte[] FtpDownLoad(string filePath) { try { FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer + ftpMapPath + filePath), WebRequestMethods.Ftp.DownloadFile); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); if (response == null) { return null; } Stream retStream = response.GetResponseStream(); List<byte> contentArray = new List<byte>(); int bt = retStream.ReadByte(); while (bt > -1) { contentArray.Add((byte)bt); bt = retStream.ReadByte(); } retStream.Dispose(); return contentArray.ToArray(); } catch (Exception ex) { LogHelper.LogError("FtpDownLoad失败!", ex); return null; } } private FtpWebRequest GetFtpWebRequest(Uri uri, string method) { FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri); ftpClientRequest.Proxy = null; ftpClientRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword); ftpClientRequest.UseBinary = true; //ftpClientRequest.UsePassive = true; ftpClientRequest.Method = method; //ftpClientRequest.Timeout = -1; return ftpClientRequest; } private bool FtpUploadFile(byte[] fileContent,string fullPath) { try { string ftpFileDir = Path.GetDirectoryName(fullPath); //这个总要执行 FtpMakeDirByPath(ftpFileDir.Replace("\\","/")); FtpWebRequest reqFTP = GetFtpWebRequest(new Uri(fptServer+fullPath), WebRequestMethods.Ftp.UploadFile); Stream stream = reqFTP.GetRequestStream(); stream.Write(fileContent, 0, fileContent.Length); stream.Flush(); stream.Close(); return true; } catch(Exception ex) { LogHelper.LogError("FtpUploadFile失败!", ex); return false; } } /// <summary> /// 根据路径创建多级目录 /// </summary> /// <param name="dirPath"></param> /// <returns></returns> private bool FtpMakeDirByPath(string dirPath) { if(string.IsNullOrEmpty(dirPath)) { return false; } dirPath=dirPath.Replace("\\", "/"); string[] dirs= dirPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (dirs != null && dirs.Length > 0) { bool result = false; string des = fptServer; try { for (int i = 0; i < dirs.Length; i++) { result = FtpMakeDir(des, dirs[i]); des = des + "/" + dirs[i]; } return true; } catch(Exception ex) { LogHelper.LogError("FtpMakeDirByPath失败!", ex); return false; } } return false; } //创建目录 private bool FtpMakeDir(string destinationPath,string dirName) { FtpWebResponse response = null; try { Uri uri = new Uri(destinationPath+"/"+ dirName); FtpWebRequest reqFTP = GetFtpWebRequest(uri, WebRequestMethods.Ftp.MakeDirectory); response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } catch (Exception ex) { LogHelper.LogError("FtpMakeDir失败!", ex); return false; } }