zoukankan      html  css  js  c++  java
  • C# WebClient进行FTP服务上传文件和下载文件

    定义WebClient使用的操作类: 操作类名称WebUpDown

    WebClient上传文件至Ftp服务:

    //// <summary> 

            /// WebClient上传文件至Ftp服务

            /// </summary> 

            /// <param name="fileNamePath">文件名,全路径格式</param> 

            /// <param name="uriString">服务器文件夹路径</param> 

            public static void UpLoadFile(string fileNamePath, string uriString)

            {

                string NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));

                uriString = uriString + NewFileName;

     

                //创建WebClient实例 

                WebClient myWebClient = new WebClient();

     

                //指定用户名和密码

                myWebClient.Credentials = new NetworkCredential("username", "password");

     

                try

                {

                    //上传文件

                    myWebClient.UploadFile(new Uri(uriString), fileNamePath);

                }

                catch (Exception ex)

                {

                    MessageBox.Show("文件上传失败,失败原因:" + ex.Message);

                }

                finally

                {

                    myWebClient.Dispose();

                }

            } 

    下载服务器文件至客户端:

            /// <summary> 

            /// 下载服务器文件至客户端 

            /// </summary> 

            /// <param name="URL">被下载的文件地址,绝对路径</param> 

            /// <param name="Dir">另存放的目录</param> 

            public static void Download(string URL, string Dir)

            {

                WebClient client = new WebClient();

                client.Credentials = new NetworkCredential("username "," password ");

                string Path = Dir;   //另存为的绝对路径+文件名 

     

                try

                {

                    client.DownloadFile(new Uri(URL), Path);

                }

                catch (Exception ex)

                {

                    MessageBox.Show("文件下载失败,失败原因:" + ex.Message);

                }

                finally

                {

                    client.Dispose();

                }

            }

    调用方法:

    /// <summary>

            /// WebClient上传到Ftp服务

            /// </summary>

            /// <param name="sender"></param>

            /// <param name="e"></param>

            private void Button_Click_5(object sender, RoutedEventArgs e)

            {

                WebUpDown.UpLoadFile(@"C:123.txt",@"ftp://localhost//");

            }

     

            /// <summary>

            /// WebClient使用Ftp服务下载到客户端

            /// </summary>

            /// <param name="sender"></param>

            /// <param name="e"></param>

            private void Button_Click_6(object sender, RoutedEventArgs e)

            {

                WebUpDown.Download(@"ftp://localhost//123.txt", @"C:123.txt");

            }

  • 相关阅读:
    NOI 2019 网络同步赛 游记
    洛谷 P3695 CYaRon!语 题解 【模拟】【字符串】
    洛谷 P2482 loj #2885 [SDOI2010]猪国杀 题解【模拟】【贪心】【搜索】
    Spring MVC @ResponseBody返回中文字符串乱码问题
    Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread
    @Value取不到值引出的spring的2种配置文件applicationContext.xml和xxx-servlet.xml
    @RestController注解下返回到jsp视图页面
    Mysql引起的spring事务失效
    Eclipse中启动tomcat报错:A child container failed during start
    xshell不能输入中文,显示为??
  • 原文地址:https://www.cnblogs.com/wlming/p/8075210.html
Copyright © 2011-2022 走看看