FileManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Configuration;
  7. using System.Web;
  8. using System.Collections.Generic;
  9. using Ionic.Zip;
  10. namespace Bowin.Common
  11. {
  12. /// <summary>
  13. /// FileManager 的摘要说明。
  14. /// </summary>
  15. public class FileManager
  16. {
  17. public string ErrorMessage;
  18. private CredentialCache myCredentialCache;
  19. private string password;
  20. private string url;
  21. private string username;
  22. public FileManager()
  23. {
  24. this.ErrorMessage = "";
  25. this.username = ConfigurationManager.AppSettings["__ImportDataFileUser__"];
  26. this.password = ConfigurationManager.AppSettings["__ImportDataFilePass__"];
  27. this.url = ConfigurationManager.AppSettings["__ImportDataFileUrl__"];
  28. if (this.url.Length > 0)
  29. {
  30. if (this.url.Substring(this.url.Length - 1, 1) == "/")
  31. {
  32. this.url = this.url;
  33. }
  34. else
  35. {
  36. this.url = this.url + "/";
  37. }
  38. }
  39. this.myCredentialCache = new CredentialCache();
  40. this.myCredentialCache.Add(new Uri(this.url), "NTLM", new NetworkCredential(this.username, this.password));
  41. }
  42. public bool DeleteFile(string filename)
  43. {
  44. this.ErrorMessage = "";
  45. HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.url + filename);
  46. request.Credentials = this.myCredentialCache;
  47. request.Method = "DELETE";
  48. WebResponse response = null;
  49. try
  50. {
  51. response = request.GetResponse();
  52. }
  53. catch (WebException ex)
  54. {
  55. this.ErrorMessage = ex.Message;
  56. return false;
  57. }
  58. finally
  59. {
  60. if (response != null)
  61. {
  62. response.Close();
  63. }
  64. }
  65. return true;
  66. }
  67. public bool IsFileExist(string filename)
  68. {
  69. this.ErrorMessage = "";
  70. string text1 = "<?xml version=\"1.0\"?><a:propfind xmlns:a=\"DAV:\"><a:prop><a:getcontentlength/></a:prop></a:propfind>";
  71. byte[] buffer1 = new ASCIIEncoding().GetBytes(text1);
  72. HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.url + filename);
  73. request.Credentials = this.myCredentialCache;
  74. request.Method = "PROPFIND";
  75. request.ContentType = "text/xml";
  76. request.Headers.Add("Depth", "0");
  77. request.Headers.Add("Translate: f");
  78. request.ContentLength = buffer1.Length;
  79. Stream stream = request.GetRequestStream();
  80. stream.Write(buffer1, 0, buffer1.Length);
  81. stream.Close();
  82. WebResponse response = null;
  83. try
  84. {
  85. response = request.GetResponse();
  86. }
  87. catch (WebException ex)
  88. {
  89. if (ex.Message.IndexOf("404") == -1)
  90. {
  91. this.ErrorMessage = ex.Message;
  92. throw ex;
  93. }
  94. return false;
  95. }
  96. finally
  97. {
  98. if (response != null)
  99. {
  100. response.Close();
  101. }
  102. }
  103. return true;
  104. }
  105. public bool UploadFile(Stream inputStream, string filename)
  106. {
  107. this.ErrorMessage = "";
  108. WebClient client = new WebClient();
  109. client.Credentials = this.myCredentialCache;
  110. int num1 = (int)inputStream.Length;
  111. byte[] buffer1 = new byte[num1];
  112. inputStream.Read(buffer1, 0, num1);
  113. Stream stream = client.OpenWrite(this.url + filename, "PUT");
  114. stream.Write(buffer1, 0, num1);
  115. try
  116. {
  117. stream.Close();
  118. }
  119. catch (WebException ex)
  120. {
  121. this.ErrorMessage = ex.Message;
  122. return false;
  123. }
  124. return true;
  125. }
  126. public bool UploadFile(byte[] buffer, string filename)
  127. {
  128. this.ErrorMessage = "";
  129. WebClient client = new WebClient();
  130. client.Credentials = this.myCredentialCache;
  131. Stream stream = client.OpenWrite(this.url + filename, "PUT");
  132. stream.Write(buffer, 0, buffer.Length);
  133. try
  134. {
  135. stream.Close();
  136. }
  137. catch (WebException ex)
  138. {
  139. this.ErrorMessage = ex.Message;
  140. return false;
  141. }
  142. return true;
  143. }
  144. public Stream ReadFile(string FileName)
  145. {
  146. Stream stream = System.IO.File.OpenRead(FileName);
  147. return stream;
  148. }
  149. /// <summary>
  150. /// 下载文件服务器中的文件
  151. /// </summary>
  152. /// <param name="virtualFilePath">目标文件的虚拟路径</param>
  153. /// <param name="remoteFile">远程文件地址</param>
  154. public static void DownloadFile(string virtualFilePath, string remoteFile)
  155. {
  156. WebClient conn = new WebClient();
  157. var targetFile = remoteFile;
  158. File.Delete(HttpContext.Current.Server.MapPath(virtualFilePath));
  159. if (!remoteFile.Trim().StartsWith("http://"))
  160. {
  161. targetFile = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
  162. if (!remoteFile.StartsWith("/"))
  163. {
  164. targetFile += "/";
  165. }
  166. targetFile += remoteFile;
  167. }
  168. conn.DownloadFile(targetFile, HttpContext.Current.Server.MapPath(virtualFilePath));
  169. }
  170. }
  171. }