• C# ftp 上传、下载、删除


     

    C# ftp 上传、下载、删除

    分类: [ 11 ] Ftp
    [csharp] view plaincopyprint?
     
    1. public class FtpHelper  
    2.    {  
    3.        public static readonly FtpHelper Instance = new FtpHelper();  
    4.   
    5.        /// <summary>  
    6.        /// 取得文件名  
    7.        /// </summary>  
    8.        /// <param name="ftpPath">ftp路径</param>  
    9.        /// <returns></returns>  
    10.        public string[] GetFilePath(string userId, string pwd, string ftpPath)  
    11.        {  
    12.            string[] downloadFiles;  
    13.            StringBuilder result = new StringBuilder();  
    14.            FtpWebRequest reqFTP;  
    15.            try  
    16.            {  
    17.                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));  
    18.                reqFTP.UseBinary = true;  
    19.                reqFTP.Credentials = new NetworkCredential(userId, pwd);  
    20.                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;  
    21.                reqFTP.UsePassive = false;  
    22.                WebResponse response = reqFTP.GetResponse();  
    23.                StreamReader reader = new StreamReader(response.GetResponseStream());  
    24.                string line = reader.ReadLine();  
    25.                while (line != null)  
    26.                {  
    27.                    result.Append(line);  
    28.                    result.Append(" ");  
    29.                    line = reader.ReadLine();  
    30.                }  
    31.                result.Remove(result.ToString().LastIndexOf(' '), 1);  
    32.                reader.Close();  
    33.                response.Close();  
    34.                return result.ToString().Split(' ');  
    35.            }  
    36.            catch (Exception ex)  
    37.            {  
    38.                downloadFiles = null;  
    39.                return downloadFiles;  
    40.            }  
    41.        }  
    42.   
    43.        //ftp的上传功能  
    44.        public void Upload(string userId, string pwd, string filename, string ftpPath)  
    45.        {  
    46.            FileInfo fileInf = new FileInfo(filename);  
    47.            FtpWebRequest reqFTP;  
    48.            // 根据uri创建FtpWebRequest对象   
    49.            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileInf.Name));  
    50.            // ftp用户名和密码  
    51.            reqFTP.Credentials = new NetworkCredential(userId, pwd);  
    52.   
    53.            reqFTP.UsePassive = false;  
    54.            // 默认为true,连接不会被关闭  
    55.            // 在一个命令之后被执行  
    56.            reqFTP.KeepAlive = false;  
    57.            // 指定执行什么命令  
    58.            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
    59.            // 指定数据传输类型  
    60.            reqFTP.UseBinary = true;  
    61.            // 上传文件时通知服务器文件的大小  
    62.            reqFTP.ContentLength = fileInf.Length;  
    63.            // 缓冲大小设置为2kb  
    64.            int buffLength = 2048;  
    65.            byte[] buff = new byte[buffLength];  
    66.            int contentLen;  
    67.            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件  
    68.            FileStream fs = fileInf.OpenRead();  
    69.            try  
    70.            {  
    71.                // 把上传的文件写入流  
    72.                Stream strm = reqFTP.GetRequestStream();  
    73.                // 每次读文件流的2kb  
    74.                contentLen = fs.Read(buff, 0, buffLength);  
    75.                // 流内容没有结束  
    76.                while (contentLen != 0)  
    77.                {  
    78.                    // 把内容从file stream 写入 upload stream  
    79.                    strm.Write(buff, 0, contentLen);  
    80.                    contentLen = fs.Read(buff, 0, buffLength);  
    81.                }  
    82.                // 关闭两个流  
    83.                strm.Close();  
    84.                fs.Close();  
    85.            }  
    86.            catch (Exception ex)  
    87.            {  
    88.   
    89.            }  
    90.        }  
    91.   
    92.        public void Delete(string userId, string pwd, string ftpPath, string fileName)  
    93.        {  
    94.            FtpWebRequest reqFTP;  
    95.            try  
    96.            {  
    97.                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));  
    98.                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;  
    99.                reqFTP.UseBinary = true;  
    100.                reqFTP.Credentials = new NetworkCredential(userId, pwd);  
    101.                reqFTP.UsePassive = false;  
    102.                FtpWebResponse listResponse = (FtpWebResponse)reqFTP.GetResponse();  
    103.                string sStatus = listResponse.StatusDescription;  
    104.            }  
    105.            catch (Exception ex)  
    106.            {  
    107.                throw ex;  
    108.            }  
    109.        }  
    110.   
    111.        //从ftp服务器上下载文件的功能  
    112.        public void Download(string userId, string pwd, string ftpPath, string filePath, string fileName)  
    113.        {  
    114.            FtpWebRequest reqFTP;  
    115.            try  
    116.            {  
    117.                FileStream outputStream = new FileStream(filePath + "\" + fileName, FileMode.Create);  
    118.                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));  
    119.                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;  
    120.                reqFTP.UseBinary = true;  
    121.                reqFTP.Credentials = new NetworkCredential(userId, pwd);  
    122.                reqFTP.UsePassive = false;  
    123.                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
    124.                Stream ftpStream = response.GetResponseStream();  
    125.                long cl = response.ContentLength;  
    126.                int bufferSize = 2048;  
    127.                int readCount;  
    128.                byte[] buffer = new byte[bufferSize];  
    129.                readCount = ftpStream.Read(buffer, 0, bufferSize);  
    130.                while (readCount > 0)  
    131.                {  
    132.                    outputStream.Write(buffer, 0, readCount);  
    133.                    readCount = ftpStream.Read(buffer, 0, bufferSize);  
    134.                }  
    135.                ftpStream.Close();  
    136.                outputStream.Close();  
    137.                response.Close();  
    138.   
    139.   
    140.            }  
    141.            catch (Exception ex)  
    142.            {  
    143.                throw ex;  
    144.            }  
    145.        }  
    146.   
    147.    }  
  • 相关阅读:
    .Net培训个人总结笔记28
    .Net培训个人总结笔记32
    .Net培训个人总结笔记21
    Codeforces Round #751 (Div. 1 & 2) Solutions
    「CEOI2017」Mousetrap 题解
    「CQOI2017」小Q的表格 题解
    mysql 非数字字段注意事项
    认识key及新鲜应用模式.
    B2C的革命: QQ网购
    php语言流程控制中的主动与被动.
  • 原文地址:https://www.cnblogs.com/aaaaq/p/4396372.html
Copyright © 2020-2023  润新知