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 Ionic.Zip;
namespace Bowin.Common
{
///
/// FileManager 的摘要说明。
///
public class FileManager
{
public string ErrorMessage;
private CredentialCache myCredentialCache;
private string password;
private string url;
private string username;
public FileManager()
{
this.ErrorMessage = "";
this.username = ConfigurationManager.AppSettings["__ImportDataFileUser__"];
this.password = ConfigurationManager.AppSettings["__ImportDataFilePass__"];
this.url = ConfigurationManager.AppSettings["__ImportDataFileUrl__"];
if (this.url.Length > 0)
{
if (this.url.Substring(this.url.Length - 1, 1) == "/")
{
this.url = this.url;
}
else
{
this.url = this.url + "/";
}
}
this.myCredentialCache = new CredentialCache();
this.myCredentialCache.Add(new Uri(this.url), "NTLM", new NetworkCredential(this.username, this.password));
}
public bool DeleteFile(string filename)
{
this.ErrorMessage = "";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.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 = "";
string text1 = "";
byte[] buffer1 = new ASCIIEncoding().GetBytes(text1);
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.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 = "";
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(this.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 = "";
WebClient client = new WebClient();
client.Credentials = this.myCredentialCache;
Stream stream = client.OpenWrite(this.url + filename, "PUT");
stream.Write(buffer, 0, buffer.Length);
try
{
stream.Close();
}
catch (WebException ex)
{
this.ErrorMessage = ex.Message;
return false;
}
return true;
}
public Stream ReadFile(string FileName)
{
Stream stream = System.IO.File.OpenRead(FileName);
return stream;
}
///
/// 下载文件服务器中的文件
///
/// 目标文件的虚拟路径
/// 远程文件地址
public static void DownloadFile(string virtualFilePath, string remoteFile)
{
WebClient conn = new WebClient();
var targetFile = remoteFile;
File.Delete(HttpContext.Current.Server.MapPath(virtualFilePath));
if (!remoteFile.Trim().StartsWith("http://"))
{
targetFile = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
if (!remoteFile.StartsWith("/"))
{
targetFile += "/";
}
targetFile += remoteFile;
}
conn.DownloadFile(targetFile, HttpContext.Current.Server.MapPath(virtualFilePath));
}
}
}