No Description

UPloadFile.cs 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Web;
  7. /// <summary>
  8. /// UPloadFile 的摘要说明
  9. /// </summary>
  10. public class UPloadFile
  11. {
  12. public string ftpPath;
  13. public string ftpdir;
  14. public string ftpUserID;
  15. public string ftpPassword;
  16. /// <summary>
  17. /// 上传文件
  18. /// </summary>
  19. /// <param name="fileinfo">需要上传的文件</param>
  20. public string UploadLocalToFtp(string localFile)
  21. {
  22. string res = "";
  23. if (!File.Exists(localFile))
  24. {
  25. res = "文件:“" + localFile + "” 不存在!";
  26. return res;
  27. }
  28. FileInfo fileInf = new FileInfo(localFile);
  29. //改后文件
  30. #region 改后文件
  31. //string temp_filename = localFile.Split('\\').Last().Split('.')[0] + "_temp";
  32. //string localfileAft = Path.GetDirectoryName(localFile) + "\\" + temp_filename + ".wav";
  33. //FileInfo fileInfAft = new FileInfo(localfileAft);
  34. #endregion
  35. string ftpURI = "ftp://" + ftpPath + "/";
  36. if (!string.IsNullOrEmpty(ftpdir))
  37. ftpURI = ftpURI + ftpdir + "/";
  38. string uri = ftpURI + fileInf.Name;
  39. //string uriAft = ftpURI + fileInfAft.Name;
  40. FtpWebRequest reqFTP;
  41. //20180209
  42. //System.GC.Collect();
  43. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象
  44. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  45. reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  46. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  47. reqFTP.UseBinary = true;// 指定数据传输类型
  48. reqFTP.UsePassive = true;
  49. reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小
  50. int buffLength = 2048;// 缓冲大小设置为2kb
  51. byte[] buff = new byte[buffLength];
  52. int contentLen;
  53. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  54. //FileStream fs = fileInf.OpenRead();
  55. using (FileStream fs = new FileStream(localFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  56. {
  57. try
  58. {
  59. res += uri;// + localFile;
  60. using (Stream strm = reqFTP.GetRequestStream())// 把上传的文件写入流
  61. {
  62. //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
  63. contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb
  64. while (contentLen != 0)// 流内容没有结束
  65. {
  66. // 把内容从file stream 写入 upload stream
  67. strm.Write(buff, 0, contentLen);
  68. contentLen = fs.Read(buff, 0, buffLength);
  69. }
  70. //StringBuilder sb = new StringBuilder();
  71. //while (!strm.EndOfStream)
  72. //{
  73. // sb.AppendLine(strm.ReadLine() + "<br>");
  74. //}
  75. }
  76. // 关闭两个流
  77. //strm.Close();
  78. fs.Close();
  79. res = "上传成功!";
  80. }
  81. catch (Exception ex)
  82. {
  83. res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
  84. }
  85. }
  86. //FtpWebRequest reqFTP1;
  87. ////20180209
  88. ////System.GC.Collect();
  89. //reqFTP1 = (FtpWebRequest)FtpWebRequest.Create(new Uri(uriAft));// 根据uri创建FtpWebRequest对象
  90. //reqFTP1.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码
  91. //reqFTP1.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行
  92. //reqFTP1.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令
  93. //reqFTP1.UseBinary = true;// 指定数据传输类型
  94. //reqFTP1.UsePassive = true;
  95. //reqFTP1.ContentLength = fileInfAft.Length;// 上传文件时通知服务器文件的大小
  96. //int buffLengthaft = 2048;// 缓冲大小设置为2kb
  97. //byte[] buffaft = new byte[buffLengthaft];
  98. //int contentLenaft;
  99. // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
  100. //FileStream fs = fileInf.OpenRead();
  101. //using (FileStream fs1 = new FileStream(localfileAft, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  102. //{
  103. // try
  104. // {
  105. // res += uriAft + localfileAft;
  106. // using (Stream strm1 = reqFTP1.GetRequestStream())// 把上传的文件写入流
  107. // {
  108. // //StreamReader strm = new StreamReader(fs, System.Text.Encoding.Default);
  109. // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);// 每次读文件流的2kb
  110. // while (contentLenaft != 0)// 流内容没有结束
  111. // {
  112. // // 把内容从file stream 写入 upload stream
  113. // strm1.Write(buffaft, 0, contentLenaft);
  114. // contentLenaft = fs1.Read(buffaft, 0, buffLengthaft);
  115. // }
  116. // //StringBuilder sb = new StringBuilder();
  117. // //while (!strm.EndOfStream)
  118. // //{
  119. // // sb.AppendLine(strm.ReadLine() + "<br>");
  120. // //}
  121. // }
  122. // // 关闭两个流
  123. // //strm.Close();
  124. // fs1.Close();
  125. // res = "上传成功!";
  126. // }
  127. // catch (Exception ex)
  128. // {
  129. // res += "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>。路径:" + localFile;
  130. // }
  131. //}
  132. return res;
  133. }
  134. /// <summary>
  135. /// 判断ftp服务器上该目录是否存在
  136. /// </summary>
  137. /// <param name="ftpPath">FTP路径目录</param>
  138. /// <param name="dirName">目录上的文件夹名称</param>
  139. /// <returns></returns>
  140. private bool CheckDirectoryExist(string ftpPath, string dirName)
  141. {
  142. bool flag = true;
  143. try
  144. {
  145. //实例化FTP连接
  146. FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
  147. ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  148. ftp.Method = WebRequestMethods.Ftp.ListDirectory;
  149. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  150. response.Close();
  151. }
  152. catch (Exception)
  153. {
  154. flag = false;
  155. }
  156. return flag;
  157. }
  158. /// <summary>
  159. /// 创建文件夹
  160. /// </summary>
  161. /// <param name="ftpPath">FTP路径</param>
  162. /// <param name="dirName">创建文件夹名称</param>
  163. public void MakeDir(string ftpPath, string dirName)
  164. {
  165. FtpWebRequest reqFTP;
  166. try
  167. {
  168. string ui = (ftpPath + dirName).Trim();
  169. reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
  170. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  171. reqFTP.UseBinary = true;
  172. reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  173. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  174. Stream ftpStream = response.GetResponseStream();
  175. ftpStream.Close();
  176. response.Close();
  177. //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
  178. }
  179. catch (Exception ex)
  180. {
  181. //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
  182. }
  183. }
  184. /// <summary>
  185. /// 下载文件
  186. /// </summary>
  187. /// <param name="serverName">服务器文件名称</param>
  188. /// <param name="localName">需要保存在本地的文件名称</param>
  189. /// <returns>下载成功返回 true</returns>
  190. //public bool Download(string serverName, string localName)
  191. //{
  192. // bool result = false;
  193. // using (FileStream fs = new FileStream(localName, FileMode.OpenOrCreate))
  194. // {
  195. // try
  196. // {
  197. // string url = UrlCombine(Host, RemotePath, serverName);
  198. // Console.WriteLine(url);
  199. // FtpWebRequest request = CreateConnection(url, WebRequestMethods.Ftp.DownloadFile);
  200. // request.ContentOffset = fs.Length;
  201. // using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
  202. // {
  203. // fs.Position = fs.Length;
  204. // byte[] buffer = new byte[1024 * 4];
  205. // int count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
  206. // while (count > 0)
  207. // {
  208. // fs.Write(buffer, 0, count);
  209. // count = response.GetResponseStream().Read(buffer, 0, buffer.Length);
  210. // }
  211. // response.GetResponseStream().Close();
  212. // }
  213. // result = true;
  214. // }
  215. // catch (WebException ex)
  216. // {
  217. // // 处理ftp连接中的异常
  218. // }
  219. // }
  220. // return result;
  221. //}
  222. }