FileManager.cs 4.9 KB

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