zdc253212956 发表于 2015-5-26 10:30:49

c#实现FTP上传

1         ///
2         /// 上传文件
3         ///
4         /// 需要上传的文件
5         /// 目标路径
6         /// ftp地址
7         /// ftp用户名
8         /// ftp密码
9         public static void UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
10         {
11             //1. check target
12             string target;
13             if (targetDir.Trim() == "")
14             {
15               return;
16             }
17             target = Guid.NewGuid().ToString();//使用临时文件名
18
19
20             string URI = "FTP://" + hostname + "/" + targetDir + "/" + target;
21             ///WebClient webcl = new WebClient();
22             System.Net.FtpWebRequest ftp = GetRequest(URI, username, password);
23
24             //设置FTP命令 设置所要执行的FTP命令,
25             //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表
26             ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
27             //指定文件传输的数据类型
28             ftp.UseBinary = true;
29             ftp.UsePassive = true;
30
31             //告诉ftp文件大小
32             ftp.ContentLength = fileinfo.Length;
33             //缓冲大小设置为2KB
34             const int BufferSize = 2048;
35             byte[] content = new byte;
36             int dataRead;
37
38             //打开一个文件流 (System.IO.FileStream) 去读上传的文件
39             using (FileStream fs = fileinfo.OpenRead())
40             {
41               try
42               {
43                     //把上传的文件写入流
44                     using (Stream rs = ftp.GetRequestStream())
45                     {
46                         do
47                         {
48                           //每次读文件流的2KB
49                           dataRead = fs.Read(content, 0, BufferSize);
50                           rs.Write(content, 0, dataRead);
51                         } while (!(dataRead < BufferSize));
52                         rs.Close();
53                     }
54
55               }
56               catch (Exception ex) { }
57               finally
58               {
59                     fs.Close();
60               }
61
62             }
63
64             ftp = null;
65             //设置FTP命令
66             ftp = GetRequest(URI, username, password);
67             ftp.Method = System.Net.WebRequestMethods.Ftp.Rename; //改名
68             ftp.RenameTo = fileinfo.Name;
69             try
70             {
71               ftp.GetResponse();
72             }
73             catch (Exception ex)
74             {
75               ftp = GetRequest(URI, username, password);
76               ftp.Method = System.Net.WebRequestMethods.Ftp.DeleteFile; //删除
77               ftp.GetResponse();
78               throw ex;
79             }
80             finally
81             {
82               //fileinfo.Delete();
83             }
84
85             // 可以记录一个日志"上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
86             ftp = null;
87
88             #region
89             /*****
90            *FtpWebResponse
91            * ****/
92             //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse();
93             #endregion
94         }
95
96         private static FtpWebRequest GetRequest(string URI, string username, string password)
97         {
98             //根据服务器信息FtpWebRequest创建类的对象
99             FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
100             //提供身份验证信息
101             result.Credentials = new System.Net.NetworkCredential(username, password);
102             //设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true
103             result.KeepAlive = false;
104             return result;
105         }
  
页: [1]
查看完整版本: c#实现FTP上传