文章參考來源地址:https://blog.csdn.net/wybshyy/article/details/52095542緩存
本次對代碼進行了一點擴展:將文件上傳到ftp指定目錄下,若目錄不存在則建立目錄。函數
調整點:在構造函數中,處理和得到可直接使用的ftpURI。spa
完整代碼以下:.net
public class FtpWeb { string ftpServerIP; string ftpRemotePath; string ftpUserID; string ftpPassword; string ftpURI; /// <summary> /// 鏈接FTP /// </summary> /// <param name="FtpServerIP">FTP鏈接地址</param> /// <param name="FtpRemotePath">指定FTP鏈接成功後的當前目錄, 若是不指定即默認爲根目錄</param> /// <param name="FtpUserID">用戶名</param> /// <param name="FtpPassword">密碼</param> public FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword) { ftpServerIP = FtpServerIP; ftpRemotePath = FtpRemotePath; ftpUserID = FtpUserID; ftpPassword = FtpPassword; //ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/"; ftpURI = CheckDirectory(); } /// <summary> /// 上傳 /// </summary> /// <param name="filename"></param> public bool Upload(string filename, out string errorMsg) { errorMsg = ""; FileInfo fileInf = new FileInfo(filename); string uri = ftpURI + fileInf.Name; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInf.Length; int buffLength = 2048; //開闢2KB緩存區 byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInf.OpenRead(); try { Stream strm = reqFTP.GetRequestStream(); contentLen = fs.Read(buff, 0, buffLength); while (contentLen != 0) { strm.Write(buff, 0, contentLen); contentLen = fs.Read(buff, 0, buffLength); } strm.Close(); fs.Close(); return true; } catch (Exception ex) { errorMsg = ex.Message; return false; } } /// <summary> /// 下載 /// </summary> /// <param name="filePath"></param> /// <param name="fileName"></param> public bool Download(string filePath, string fileName, out string errorMsg) { errorMsg = ""; FtpWebRequest reqFTP; try { FileStream outputStream = new FileStream(filePath + "//" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); return true; } catch (Exception ex) { errorMsg = ex.Message; return false; } } #region 私有 /// <summary> /// 得到+創建文件所在物理路徑 /// </summary> /// <returns></returns> private string CheckDirectory() { string dir = "ftp://" + ftpServerIP + "/";//根 if (!string.IsNullOrWhiteSpace(ftpRemotePath)) { var folderArr = ftpRemotePath.Split(new string[] { @"\", @"/" }, StringSplitOptions.RemoveEmptyEntries); if (folderArr != null && folderArr.Length > 0) { foreach (var folder in folderArr) { dir = dir + folder + "/"; MakeDir(dir); } } } return dir; } /// <summary> /// 建立目錄 /// </summary> /// <param name="errorMsg"></param> /// <returns></returns> private string MakeDir(string dir) { string errorMsg = ""; FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(dir)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; reqFTP.KeepAlive = false; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); } catch (Exception ex) { errorMsg = "建立文件失敗,緣由: " + ex.Message; } return errorMsg; } #endregion }