zoukankan      html  css  js  c++  java
  • 下载(二)客户端通过C# 代码直接从服务器下载文件到本地

    • 局域网文件下载:

    public class RemoteDownload
        {
            public static void DownLoad(string addressUrl,string localName)
            {
                //下载文件
                System.Net.WebClient myWebClient = new System.Net.WebClient();
                myWebClient.DownloadFile(@"/10.2.0.254/software/01279.lic.txt", "testdownload.txt");           
                //下载end
            }
        }

    • 通过web方式,从远程服务器端下载文件:

    public class WebDownload
        {
            public static void DownLoad(string Url, string FileName)
            {
                bool Value = false;
                WebResponse response = null;
                Stream stream = null;

                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

                    response = request.GetResponse();
                    stream = response.GetResponseStream();

                    if (!response.ContentType.ToLower().StartsWith("text/"))
                    {
                        Value = SaveBinaryFile(response, FileName);

                    }

                }
                catch (Exception err)
                {
                    string aa = err.ToString();
                }

            }

            /// <summary>
            /// Save a binary file to disk.
            /// </summary>
            /// <param name="response">The response used to save the file</param>
            // 将二进制文件保存到磁盘
            private static bool SaveBinaryFile(WebResponse response, string FileName)
            {
                bool Value = true;
                byte[] buffer = new byte[1024];

                try
                {
                    if (File.Exists(FileName))
                        File.Delete(FileName);
                    Stream outStream = System.IO.File.Create(FileName);
                    Stream inStream = response.GetResponseStream();

                    int l;
                    do
                    {
                        l = inStream.Read(buffer, 0, buffer.Length);
                        if (l > 0)
                            outStream.Write(buffer, 0, l);
                    }
                    while (l > 0);

                    outStream.Close();
                    inStream.Close();
                }
                catch
                {
                    Value = false;
                }
                return Value;
            }

    • 从FTP上下载文件:

     public class FtpDownload
        {
            public static void DownLoad(string FtpPath)
            {
                /*首先从配置文件读取ftp的登录信息*/
                string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
                string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
                string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
                string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();


                Uri uri = new Uri(FtpPath);
                string FileName = Path.GetFullPath(TempFolderPath) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);

                //创建一个文件流
                FileStream fs = null;
                Stream responseStream = null;
                try
                {
                    //创建一个与FTP服务器联系的FtpWebRequest对象
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                    //设置请求的方法是FTP文件下载
                    request.Method = WebRequestMethods.Ftp.DownloadFile;

                    //连接登录FTP服务器
                    request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);

                    //获取一个请求响应对象
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    //获取请求的响应流
                    responseStream = response.GetResponseStream();

                    //判断本地文件是否存在,如果存在,则打开和重写本地文件

                    if (File.Exists(FileName))
                    {
                        if (LocalFileExistsOperation == "write")
                        {
                            fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);

                        }
                    }

                    //判断本地文件是否存在,如果不存在,则创建本地文件
                    else
                    {
                        fs = File.Create(FileName);
                    }

                    if (fs != null)
                    {

                        int buffer_count = 65536;
                        byte[] buffer = new byte[buffer_count];
                        int size = 0;
                        while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
                        {
                            fs.Write(buffer, 0, size);

                        }
                        fs.Flush();
                        fs.Close();
                        responseStream.Close();
                    }
                }
                finally
                {
                    if (fs != null)
                        fs.Close();
                    if (responseStream != null)
                        responseStream.Close();
                }


            }
        }

  • 相关阅读:
    六 . 爬虫 Xpath介绍和使用
    五. 爬虫 正则表达式
    四 . 爬虫 BeautifulSoup库参数和使用
    三 . 爬虫 url编码
    一 . 爬虫
    【HDU5952】Counting Cliques
    【HDU5521】Meeting
    【模板】回文自动机
    【CF1218E】Product Tuples
    【洛谷P2485】计算器
  • 原文地址:https://www.cnblogs.com/xiarifeixue/p/2059853.html
Copyright © 2011-2022 走看看