zoukankan      html  css  js  c++  java
  • c#实现 ftp ;http;共享方式下载文件 并对比本地文件和服务器文件的更新时间 判断性下载

    近段时间在研究这个下载 在网上找了一些列子看了看 决定把这几种下载综合起来发一个……

    //从ftp服务器上下载文件的功能  
            public void Download(string ftpServerIP, string ftpUserID, string ftpPassword, string fileName, string Destination)  
            {  
                FtpWebRequest reqFTP;  
                try  
                {  
                    FileStream outputStream = new FileStream(Destination + "\\" + fileName, FileMode.Create);  
                    // 根据uri创建FtpWebRequest对象   
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));  
                    // 指定执行什么命令  
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
                    // 默认为true,连接不会被关闭  
                    reqFTP.UseBinary = true;  
                    // ftp用户名和密码  
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
                    FtpWebResponse ftpresponse = (FtpWebResponse)reqFTP.GetResponse();  
                    Stream ftpStream = ftpresponse.GetResponseStream();  
                    long cl = ftpresponse.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();  
                    ftpresponse.Close();  
                }  
                catch (Exception ex)  
                {  
      
      
                }  
            }  
            //从http服务器上下载文件的功能  
            //为服务器路径server_path   
            //本地存储路径local_path  
            public void Download(string server_path, string local_path)  
            {  
                HttpWebRequest req;  
                try  
                {  
                    string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));  
                    if (!Directory.Exists(folder))  
                        Directory.CreateDirectory(folder);  
                    // 根据uri创建FtpWebRequest对象   
                    req = (HttpWebRequest)HttpWebRequest.Create(new Uri(server_path));  
                    // 指定执行什么命令  
                    req.Method = WebRequestMethods.Http.Get;  
                    HttpWebResponse response = (HttpWebResponse)req.GetResponse();  
                    if (File.Exists(local_path))  
                    {  
                        FileInfo localFile = new FileInfo(local_path);  
                        if (localFile.LastWriteTime > response.LastModified) return;  
                    }  
                    FileStream outputStream = new FileStream(local_path, FileMode.Create);  
                    Stream stream = response.GetResponseStream();  
                    long cl = response.ContentLength;  
                    int bufferSize = 2048;  
                    int readCount;  
                    byte[] buffer = new byte[bufferSize];  
                    readCount = stream.Read(buffer, 0, bufferSize);  
                    while (readCount > 0)  
                    {  
                        outputStream.Write(buffer, 0, readCount);  
                        readCount = stream.Read(buffer, 0, bufferSize);  
                    }  
                    stream.Close();  
                    outputStream.Close();  
                    response.Close();  
                }  
                catch (Exception ex)  
                {  
                    string s = ex.Message;  
                }  
            }  
      
      
      
      
            //从共享文件夹下载  
            public void Download(string server_path, string local_path, int i)  
            {  
                try  
                {  
                    server_path = "\\" + server_path;  
                    string folder = local_path.Substring(0, local_path.LastIndexOf("\\"));  
                    if (!Directory.Exists(folder))  
                        Directory.CreateDirectory(folder);  
                    if (!File.Exists(server_path)) return;  
      
      
                    //服务器文件信息  
                    FileInfo serverInfo = new FileInfo(server_path);  
                    DateTime serverTime = serverInfo.LastWriteTime;  
                    if (File.Exists(local_path))  
                    {  
                        FileInfo localinfo = new FileInfo(local_path);  
                        DateTime localTime = localinfo.LastWriteTime;  
      
      
                        if (localTime >= serverTime)  
                            return;  
                    }  
      
      
                    serverInfo.CopyTo(local_path, true);  
                    return;  
                }  
                catch (Exception ex)  
                {  
                    string s = ex.Message;  
                }  
            }

     

  • 相关阅读:
    移动端页面使用rem布局
    Vue2.0 render:h => h(App)
    sublime3 快捷键大全
    node-webkit学习之【无边框窗口用JS实现拖动改变大小等】
    mock.js-无需等待,随机产生数据,让前端独立于后端进行开发
    Sublime Text 3 遇到的一些小坑的解决方法
    Angular Cli 升级到最新版本
    Angular 表单嵌套、动态表单
    angular6 iframe应用
    JS 时间格式 相互转化
  • 原文地址:https://www.cnblogs.com/Fengsp/p/2625149.html
Copyright © 2011-2022 走看看