• WinScp上传和下载


    不多说,贴代码,看不懂得可以留言。需要引入WinSCP

     1 public class WebWinScp
     2     {
     3         //远程上传路径
     4         private SessionOptions sessionOptions = null;
     5         private WinSCP.Session session = new WinSCP.Session();
     6         /// <summary>
     7         /// 
     8         /// </summary>
     9         /// <param name="HostName">FTP服务器</param>
    10         /// <param name="UserName">账号</param>
    11         /// <param name="Password">密码</param>
    12         /// <param name="SshPrivateKeyPath">Ssh私有密钥路径(以PPK结尾的文件)</param>
    13         public WebWinScp(string HostName, string UserName, string Password, string SshPrivateKeyPath)
    14         {
    15             sessionOptions = new SessionOptions
    16             {
    17                 Protocol = Protocol.Sftp,
    18                 HostName = HostName,
    19                 UserName = UserName,
    20                 Password = Password,
    21                 SshPrivateKeyPath = SshPrivateKeyPath,
    22                 GiveUpSecurityAndAcceptAnySshHostKey = true
    23             };
    24         }
    25         /// <summary>
    26         /// 下载FTP上的所有文件,并移除FTP上下载的问题
    27         /// </summary>
    28         /// <param name="localPath">本地下载路径</param>
    29         /// <param name="remoteDoPath">远程下载路径</param>
    30         public void Download(string localPath, string remoteDoPath)
    31         {
    32             try
    33             {
    34                 //开启连接
    35                 session.Open(sessionOptions);
    36                 TransferOptions transferOptions = new TransferOptions();
    37                 //传输模式  二进制
    38                 transferOptions.TransferMode = TransferMode.Binary;
    39                 //FTP上的目录列表
    40                 RemoteDirectoryInfo directoryInfo = session.ListDirectory(remoteDoPath);
    41                 RemoteFileInfoCollection ct = directoryInfo.Files;
    42                 TransferOperationResult transferResult;
    43                 foreach (RemoteFileInfo item in ct)
    44                 {
    45                     transferResult = session.GetFiles(remoteDoPath + item.Name, localPath, true, transferOptions);
    46                     if (transferResult != null)
    47                     {
    48                         transferResult.Check();
    49                     }
    50                 }
    51             }
    52             catch (Exception)
    53             {
    54                 throw;
    55             }
    56             finally
    57             {
    58                 if (session.Opened)
    59                 {
    60                     session.Close();
    61                 }
    62             }
    63         }
    64         /// <summary>
    65         /// 上传本地文件到FTP上,上传成功删除本机文件
    66         /// </summary>
    67         /// <param name="localPath">本地上传路径</param>
    68         /// <param name="remoteUpPath">远程上传路径</param>
    69         public void Upload(string localPath, string remoteUpPath)
    70         {
    71             try
    72             {
    73                 //开启连接
    74                 session.Open(sessionOptions);
    75                 TransferOptions transferOptions = new TransferOptions();
    76                 //传输模式  二进制
    77                 transferOptions.TransferMode = TransferMode.Binary;
    78                 TransferOperationResult transferResult = null;
    79                 transferResult = session.PutFiles(localPath, remoteUpPath, true, transferOptions);
    80                 if (transferResult != null)
    81                 {
    82                     transferResult.Check();
    83                 }
    84             }
    85             catch (Exception)
    86             {
    87                 throw;
    88             }
    89             finally
    90             {
    91                 if (session.Opened)
    92                 {
    93                     session.Close();
    94                 }
    95             }
    96         }
    97     }
  • 相关阅读:
    js根据ip地址获取城市地理位置
    vue-cli项目中使用mock结合axios-mock-adapter生成模拟数据
    将图片转canvas
    判断浏览器是否联网
    头尾固定中间高度自适应布局 css
    经典闭包
    多余文字转化为省略号css
    $.grep()
    node 图片转base64
    CentOS 7下安装Mysql 5.7
  • 原文地址:https://www.cnblogs.com/lvphon/p/5609421.html
Copyright © 2020-2023  润新知