123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System;
- using System.IO;
- using System.Net;
- using System.Text;
- using System.Xml;
- using System.Configuration;
- using System.Web;
- using System.Collections.Generic;
- using Bowin.Common.Utility;
- using Microsoft.AspNetCore.Http.Extensions;
- using Microsoft.Extensions.Configuration;
- namespace Bowin.Common.Files
- {
- internal class FileServerSettings
- {
- public string UserName { get; set; }
- public string Password { get; set; }
- public string Url { get; set; }
- }
- /// <summary>
- /// 处理远程附件的类,由于联达并没有建立文件服务器,需要兼容:
- /// 在web调用,为了减少网络流量,就直接处理本地路径的文件;
- /// 在服务调用,配置Url、用户名密码后,处理的是远程文件服务器的文件。
- /// </summary>
- public class FileServerManager
- {
- public string ErrorMessage;
- private CredentialCache myCredentialCache = null;
- private static FileServerSettings Settings;
- private static bool IsWebLocal
- {
- get
- {
- return (HttpHelper.Accessor?.HttpContext != null || string.IsNullOrEmpty(Settings.Url));
- }
- }
- static FileServerManager()
- {
- var configuration = Utility.Environment.GetConfig("appsettings.json");
- Settings = configuration.GetSection("FileServer").Get<FileServerSettings>();
- if (Settings == null)
- {
- Settings = new FileServerSettings();
- }
- if (!string.IsNullOrEmpty(Settings.Url))
- {
- if (Settings.Url.Substring(Settings.Url.Length - 1, 1) != "/")
- {
- Settings.Url = Settings.Url + "/";
- }
- }
- }
- public FileServerManager()
- {
- this.ErrorMessage = "";
- if (!string.IsNullOrEmpty(Settings.UserName))
- {
- this.myCredentialCache = new CredentialCache();
- this.myCredentialCache.Add(new Uri(Settings.Url), "NTLM", new NetworkCredential(Settings.UserName, Settings.Password));
- }
- }
- public bool DeleteFile(string filename)
- {
- this.ErrorMessage = "";
- if (IsWebLocal)
- {
- this.ErrorMessage = "";
- try
- {
- File.Delete(HttpHelper.MapPath(filename));
- }
- catch (Exception ex)
- {
- this.ErrorMessage = ex.Message;
- return false;
- }
- }
- else
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Settings.Url + filename);
- request.Credentials = this.myCredentialCache;
- request.Method = "DELETE";
- WebResponse response = null;
- try
- {
- response = request.GetResponse();
- }
- catch (WebException ex)
- {
- this.ErrorMessage = ex.Message;
- return false;
- }
- finally
- {
- if (response != null)
- {
- response.Close();
- }
- }
- }
- return true;
- }
- public bool IsFileExist(string filename)
- {
- this.ErrorMessage = "";
- if (IsWebLocal)
- {
- return File.Exists(HttpHelper.MapPath(filename));
- }
- else
- {
- string text1 = "<?xml version=\"1.0\"?><a:propfind xmlns:a=\"DAV:\"><a:prop><a:getcontentlength/></a:prop></a:propfind>";
- byte[] buffer1 = new ASCIIEncoding().GetBytes(text1);
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Settings.Url + filename);
- request.Credentials = this.myCredentialCache;
- request.Method = "PROPFIND";
- request.ContentType = "text/xml";
- request.Headers.Add("Depth", "0");
- request.Headers.Add("Translate: f");
- request.ContentLength = buffer1.Length;
- Stream stream = request.GetRequestStream();
- stream.Write(buffer1, 0, buffer1.Length);
- stream.Close();
- WebResponse response = null;
- try
- {
- response = request.GetResponse();
- }
- catch (WebException ex)
- {
- if (ex.Message.IndexOf("404") == -1)
- {
- this.ErrorMessage = ex.Message;
- throw ex;
- }
- return false;
- }
- finally
- {
- if (response != null)
- {
- response.Close();
- }
- }
- }
- return true;
- }
- public bool UploadFile(Stream inputStream, string filename)
- {
- this.ErrorMessage = "";
- if (IsWebLocal)
- {
- using (var stream = System.IO.File.Create(HttpHelper.MapPath(filename)))
- {
- inputStream.CopyTo(stream);
- }
- }
- else
- {
- WebClient client = new WebClient();
- client.Credentials = this.myCredentialCache;
- int num1 = (int)inputStream.Length;
- byte[] buffer1 = new byte[num1];
- inputStream.Read(buffer1, 0, num1);
- Stream stream = client.OpenWrite(Settings.Url + filename, "PUT");
- stream.Write(buffer1, 0, num1);
- try
- {
- stream.Close();
- }
- catch (WebException ex)
- {
- this.ErrorMessage = ex.Message;
- return false;
- }
- }
- return true;
- }
- public bool UploadFile(byte[] buffer, string filename)
- {
- this.ErrorMessage = "";
- if (IsWebLocal)
- {
- using (var stream = System.IO.File.Create(HttpHelper.MapPath(filename)))
- {
- stream.Write(buffer);
- }
- }
- else
- {
- WebClient client = new WebClient();
- client.Credentials = this.myCredentialCache;
- Stream stream = client.OpenWrite(Settings.Url + filename, "PUT");
- stream.Write(buffer, 0, buffer.Length);
- try
- {
- stream.Close();
- }
- catch (WebException ex)
- {
- this.ErrorMessage = ex.Message;
- return false;
- }
- }
- return true;
- }
- public static Stream GetFileStream(string FileName)
- {
- if (IsWebLocal)
- {
- return FileHelper.GetFileStream(FileName);
- }
- else
- {
- WebClient conn = new WebClient();
- var targetFile = FileName;
- if (!FileName.Trim().StartsWith("http") && !FileName.Trim().Contains("://"))
- {
- targetFile = new Uri(Settings.Url).GetLeftPart(UriPartial.Authority);
- if (!FileName.StartsWith("/"))
- {
- targetFile += "/";
- }
- targetFile += FileName;
- }
- var bytes = conn.DownloadData(targetFile);
- return new MemoryStream(bytes);
- }
- }
- /// <summary>
- /// 下载文件服务器中的文件
- /// </summary>
- /// <param name="virtualFilePath">目标文件的虚拟路径</param>
- /// <param name="remoteFile">远程文件地址</param>
- public static void DownloadFile(string virtualFilePath, string remoteFile)
- {
- WebClient conn = new WebClient();
- var targetFile = remoteFile;
- File.Delete(HttpHelper.MapPath(virtualFilePath));
- if (!remoteFile.Trim().StartsWith("http") && !remoteFile.Trim().Contains("://"))
- {
- targetFile = new Uri(HttpHelper.Current.Request.GetDisplayUrl()).GetLeftPart(UriPartial.Authority);
- if (!remoteFile.StartsWith("/"))
- {
- targetFile += "/";
- }
- targetFile += remoteFile;
- }
- conn.DownloadFile(targetFile, HttpHelper.MapPath(virtualFilePath));
- }
- }
- }
|