1、操作類
/// <summary> /// SFTP操作類 /// </summary> public class SFTPHelper { #region 字段或屬性 private SftpClient sftp; private string cmsimagesIP = ConfigurationManager.AppSettings["cmsimagesIP"].ToString(); private string cmsimagesName = ConfigurationManager.AppSettings["cmsimagesName"].ToString(); private string cmsimagesPwd = ConfigurationManager.AppSettings["cmsimagesPwd"].ToString(); /// <summary> /// SFTP連接狀態 /// </summary> public bool Connected { get { return sftp.IsConnected; } } #endregion #region 構造 /// <summary> /// 構造 /// </summary> public SFTPHelper() { sftp = new SftpClient(cmsimagesIP, 22, cmsimagesName, cmsimagesPwd); } #endregion #region 連接SFTP /// <summary> /// 連接SFTP /// </summary> /// <returns>true成功</returns> public bool Connect() { try { if (!Connected) { sftp.Connect(); } return true; } catch (Exception ex) { // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("連接SFTP失敗,原因:{0}", ex.Message)); throw new Exception(string.Format("連接SFTP失敗,原因:{0}", ex.Message)); } } #endregion #region 斷開SFTP /// <summary> /// 斷開SFTP /// </summary> public void Disconnect() { try { if (sftp != null && Connected) { sftp.Disconnect(); } } catch (Exception ex) { // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("斷開SFTP失敗,原因:{0}", ex.Message)); throw new Exception(string.Format("斷開SFTP失敗,原因:{0}", ex.Message)); } } #endregion #region SFTP上傳文件 /// <summary> /// SFTP上傳文件 /// </summary> /// <param name="localPath">本地路徑</param> /// <param name="remotePath">遠程路徑</param> public void Put(string localPath, string remotePath, string fileName) { try { using (var file = File.OpenRead(localPath)) { Connect(); //判斷路徑是否存在 if (!sftp.Exists(remotePath)) { sftp.CreateDirectory(remotePath); } sftp.UploadFile(file, remotePath + fileName); Disconnect(); } } catch (Exception ex) { throw new Exception(string.Format("SFTP文件上傳失敗,原因:{0}", ex.Message)); } } #endregion #region SFTP獲取文件 /// <summary> /// SFTP獲取文件 /// </summary> /// <param name="remotePath">遠程路徑</param> /// <param name="localPath">本地路徑</param> public void Get(string remotePath, string localPath) { try { Connect(); var byt = sftp.ReadAllBytes(remotePath); Disconnect(); File.WriteAllBytes(localPath, byt); } catch (Exception ex) { throw new Exception(string.Format("SFTP文件獲取失敗,原因:{0}", ex.Message)); } } #endregion #region 獲取SFTP文件列表 /// <summary> /// 獲取SFTP文件列表 /// </summary> /// <param name="remotePath">遠程目錄</param> /// <param name="fileSuffix">文件后綴</param> /// <returns></returns> public ArrayList GetFileList(string remotePath, string fileSuffix) { try { Connect(); var files = sftp.ListDirectory(remotePath); Disconnect(); var objList = new ArrayList(); foreach (var file in files) { string name = file.Name; if (name.Length > (fileSuffix.Length + 1) && fileSuffix == name.Substring(name.Length - fileSuffix.Length)) { objList.Add(name); } } return objList; } catch (Exception ex) { // TxtLog.WriteTxt(CommonMethod.GetProgramName(), string.Format("SFTP文件列表獲取失敗,原因:{0}", ex.Message)); throw new Exception(string.Format("SFTP文件列表獲取失敗,原因:{0}", ex.Message)); } } #endregion #region 移動SFTP文件 /// <summary> /// 移動SFTP文件 /// </summary> /// <param name="oldRemotePath">舊遠程路徑</param> /// <param name="newRemotePath">新遠程路徑</param> public void Move(string oldRemotePath, string newRemotePath) { try { Connect(); sftp.RenameFile(oldRemotePath, newRemotePath); Disconnect(); } catch (Exception ex) { throw new Exception(string.Format("SFTP文件移動失敗,原因:{0}", ex.Message)); } } #endregion #region 刪除SFTP文件 public void Delete(string remoteFile) { try { Connect(); sftp.Delete(remoteFile); Disconnect(); } catch (Exception ex) { throw new Exception(string.Format("SFTP文件刪除失敗,原因:{0}", ex.Message)); } } #endregion #region 創建目錄 /// <summary> /// 循環創建目錄 /// </summary> /// <param name="remotePath">遠程目錄</param> private void CreateDirectory(string remotePath) { try { string[] paths = remotePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string curPath = "/"; for (int i = 0; i < paths.Length; i++) { curPath += paths[i]; if (!sftp.Exists(curPath)) { sftp.CreateDirectory(curPath); } if (i < paths.Length - 1) { curPath += "/"; } } } catch (Exception ex) { throw new Exception(string.Format("創建目錄失敗,原因:{0}", ex.Message)); } } #endregion }
2、調用
/// <summary> /// 上傳圖片 /// </summary> /// <param name="file"></param> /// <returns></returns> public ActionResult UploadImage(int HotelID, HttpPostedFileBase file) { if (file == null) { return Json(new { code = 0, message = "請先選擇圖片" }); } string batchNo = System.Guid.NewGuid().ToString(); string fileWebPath = "/upload/" + HotelID + "/"; string directory = Request.MapPath("~" + fileWebPath); string fileName = batchNo + Path.GetExtension(file.FileName); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string fileFullName = Path.Combine(directory, fileName); try { file.SaveAs(fileFullName); //上傳SFTP SFTPHelper sftp = new SFTPHelper(); sftp.Put(fileFullName, "/home/cmsimages/ads/cmsimages/choice/" + HotelID + "/", fileName); return Json(new { code = 1, message = fileWebPath + fileName }); } catch (Exception e) { return Json(new { code = 0, message = e.Message }); } }
3、Renci.SshNet.dll下載鏈接:
https://download.csdn.net/download/jiduxiaozhang12345/10695019
