FTP方式实现上传下载功能

来源:互联网 发布:淘宝里卖家正在被处罚 编辑:程序博客网 时间:2024/06/13 10:54

  1. 1:FTP方式。通过在IIS里架设FTP服务器。代码  
  2.   
  3. using System;  
  4. using System.Collections.Generic;  
  5. using System.Text;  
  6. using System.Net;  
  7. using System.IO;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace net.emice.TEST1  
  11. {  
  12.     class FtpUpDown  
  13.     {  
  14.    string ftpServerIP;  
  15.   
  16.    public string FtpServerIP  
  17.    {  
  18.     get { return ftpServerIP; }  
  19.     set { ftpServerIP = value; }  
  20.    }  
  21.    string ftpUserID;  
  22.   
  23.    public string FtpUserID  
  24.    {  
  25.     get { return ftpUserID; }  
  26.     set { ftpUserID = value; }  
  27.    }  
  28.    string ftpPassword;  
  29.   
  30.    public string FtpPassword  
  31.    {  
  32.     get { return ftpPassword; }  
  33.     set { ftpPassword = value; }  
  34.    }  
  35.         FtpWebRequest reqFTP;  
  36.   
  37.         private void Connect(String path)//连接ftp  
  38.         {  
  39.             // 根据uri创建FtpWebRequest对象  
  40.             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));  
  41.             // 指定数据传输类型  
  42.             reqFTP.UseBinary = true;  
  43.             // ftp用户名和密码  
  44.             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  45.         }  
  46.         public FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)  
  47.         {  
  48.             this.ftpServerIP = ftpServerIP;  
  49.             this.ftpUserID = ftpUserID;  
  50.             this.ftpPassword = ftpPassword;  
  51.         }  
  52.         //都调用这个  
  53.         private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表  
  54.         {  
  55.             string[] downloadFiles;  
  56.             StringBuilder result = new StringBuilder();  
  57.             try  
  58.             {  
  59.                 Connect(path);  
  60.                 reqFTP.Method = WRMethods;  
  61.                 WebResponse response = reqFTP.GetResponse();  
  62.                 StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名  
  63.                 string line = reader.ReadLine();  
  64.                 while (line != null)  
  65.                 {  
  66.                     result.Append(line);  
  67.                     result.Append("/n");  
  68.                     line = reader.ReadLine();  
  69.                 }  
  70.                 // to remove the trailing '/n'  
  71.                 result.Remove(result.ToString().LastIndexOf('/n'), 1);  
  72.                 reader.Close();  
  73.                 response.Close();  
  74.                 return result.ToString().Split('/n');  
  75.             }  
  76.             catch (Exception ex)  
  77.             {  
  78.                 System.Windows.Forms.MessageBox.Show(ex.Message);  
  79.                 downloadFiles = null;  
  80.                 return downloadFiles;  
  81.             }  
  82.         }  
  83.         public string[] GetFileList(string path)//上面的代码示例了如何从ftp服务器上获得文件列表  
  84.         {  
  85.             return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectory);  
  86.         }  
  87.         public string[] GetFileList()//上面的代码示例了如何从ftp服务器上获得文件列表  
  88.         {  
  89.             return GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectory);  
  90.         }  
  91.   
  92.         public string Upload(string filename) //上面的代码实现了从ftp服务器上载文件的功能  
  93.         {  
  94.             FileInfo fileInf = new FileInfo(filename);  
  95.             string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  96.             Connect(uri);//连接           
  97.             // 默认为true,连接不会被关闭  
  98.             // 在一个命令之后被执行  
  99.             reqFTP.KeepAlive = false;  
  100.             // 指定执行什么命令  
  101.             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;  
  102.             // 上传文件时通知服务器文件的大小  
  103.             reqFTP.ContentLength = fileInf.Length;  
  104.             // 缓冲大小设置为kb  
  105.             int buffLength = 2048;  
  106.             byte[] buff = new byte[buffLength];  
  107.             int contentLen;  
  108.             // 打开一个文件流(System.IO.FileStream) 去读上传的文件  
  109.             FileStream fs = fileInf.OpenRead();  
  110.             try  
  111.             {  
  112.                 // 把上传的文件写入流  
  113.                 Stream strm = reqFTP.GetRequestStream();  
  114.                 // 每次读文件流的kb  
  115.                 contentLen = fs.Read(buff, 0, buffLength);  
  116.                 // 流内容没有结束  
  117.                 while (contentLen != 0)  
  118.                 {  
  119.                    // 把内容从file stream 写入upload stream  
  120.                     strm.Write(buff, 0, contentLen);  
  121.                     contentLen = fs.Read(buff, 0, buffLength);  
  122.                 }  
  123.                 // 关闭两个流  
  124.                 strm.Close();  
  125.                 fs.Close();  
  126.      return "";  
  127.             }  
  128.            catch (Exception ex)  
  129.            {  
  130.                 return "Upload Error"+ex.Message;  
  131.             }  
  132.         }  
  133.         public string Download(string filePath, string fileName)////上面的代码实现了从ftp服务器下载文件的功能  
  134.         {  
  135.             try  
  136.             {  
  137.                 String onlyFileName = Path.GetFileName(fileName);  
  138.                 string newFileName = filePath + "//" + onlyFileName;  
  139.                 if (File.Exists(newFileName))  
  140.                 {  
  141.       return "本地文件" + newFileName + "已存在,无法下载";  
  142.                 }  
  143.                 string url = "ftp://" + ftpServerIP + "/" + fileName;  
  144.                 Connect(url);//连接   
  145.                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);  
  146.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  147.                 Stream ftpStream = response.GetResponseStream();  
  148.                 long cl = response.ContentLength;  
  149.                 int bufferSize = 2048;  
  150.                 int readCount;  
  151.                 byte[] buffer = new byte[bufferSize];  
  152.                 readCount = ftpStream.Read(buffer, 0, bufferSize);  
  153.                 FileStream outputStream = new FileStream(newFileName, FileMode.Create);  
  154.                 while (readCount > 0)  
  155.                 {  
  156.                     outputStream.Write(buffer, 0, readCount);  
  157.                     readCount = ftpStream.Read(buffer, 0, bufferSize);  
  158.                 }  
  159.                 ftpStream.Close();  
  160.                 outputStream.Close();  
  161.                 response.Close();  
  162.   
  163.                 return "";  
  164.             }  
  165.            catch (Exception ex)  
  166.             {   
  167.                 return "因"+ex.Message+",无法下载";  
  168.             }  
  169.         }  
  170.         //删除文件  
  171.         public void DeleteFileName(string fileName)  
  172.         {  
  173.            try  
  174.             {  
  175.                 FileInfo fileInf = new FileInfo(fileName);  
  176.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  177.                 Connect(uri);//连接           
  178.                 // 默认为true,连接不会被关闭  
  179.                 // 在一个命令之后被执行  
  180.                 reqFTP.KeepAlive = false;  
  181.                 // 指定执行什么命令  
  182.                 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;  
  183.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  184.                response.Close();  
  185.             }  
  186.             catch (Exception ex)  
  187.             {  
  188.                 MessageBox.Show(ex.Message, "删除错误");  
  189.             }  
  190.         }  
  191.         //创建目录  
  192.         public string MakeDir(string dirName)  
  193.         {  
  194.            try  
  195.             {  
  196.                string uri = "ftp://" + ftpServerIP + "/" + dirName;  
  197.                 Connect(uri);//连接        
  198.                 reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;  
  199.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  200.                 response.Close();  
  201.      return "";  
  202.             }  
  203.             catch (WebException ex)  
  204.             {  
  205.                 return "[Make Dir]"+ex.Message;  
  206.             }  
  207.         }  
  208.         //删除目录  
  209.         public void delDir(string dirName)  
  210.         {  
  211.            try  
  212.             {  
  213.                string uri = "ftp://" + ftpServerIP + "/" + dirName;  
  214.                 Connect(uri);//连接        
  215.                 reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;  
  216.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  217.                 response.Close();  
  218.             }  
  219.             catch (Exception ex)  
  220.             {  
  221.                 MessageBox.Show(ex.Message);  
  222.             }  
  223.         }  
  224.         //获得文件大小  
  225.         public long GetFileSize(string filename)  
  226.         {  
  227.             long fileSize = 0;  
  228.             try  
  229.             {  
  230.                 FileInfo fileInf = new FileInfo(filename);  
  231.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  232.                 Connect(uri);//连接        
  233.                 reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;  
  234.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  235.                 fileSize = response.ContentLength;  
  236.                 response.Close();  
  237.             }  
  238.             catch (Exception ex)  
  239.             {  
  240.                 MessageBox.Show(ex.Message);  
  241.             }  
  242.             return fileSize;  
  243.         }  
  244.         //文件改名  
  245.         public void Rename(string currentFilename, string newFilename)  
  246.         {  
  247.             try  
  248.             {  
  249.                 FileInfo fileInf = new FileInfo(currentFilename);  
  250.                 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;  
  251.                 Connect(uri);//连接  
  252.                 reqFTP.Method = WebRequestMethods.Ftp.Rename;  
  253.                 reqFTP.RenameTo = newFilename;  
  254.                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();  
  255.                 //Stream ftpStream = response.GetResponseStream();  
  256.                 //ftpStream.Close();  
  257.                 response.Close();  
  258.             }  
  259.             catch (Exception ex)  
  260.             {  
  261.                 MessageBox.Show(ex.Message);  
  262.             }  
  263.         }  
  264.         //获得文件明晰  
  265.         public string[] GetFilesDetailList()  
  266.         {  
  267.             return GetFileList("ftp://" + ftpServerIP + "/", WebRequestMethods.Ftp.ListDirectoryDetails);  
  268.         }  
  269.         //获得文件明晰  
  270.         public string[] GetFilesDetailList(string path)  
  271.        {  
  272.             return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);  
  273.         }  
  274.     }  
  275. }  

转载自:http://blog.csdn.net/sminhrosunny/article/details/6556655

0 0
原创粉丝点击