using System;
using System.IO;
using System.Net;
namespace ConsoleApp
{
class Program
{
public const string OCT_STREAM_CONTETNT_TYPE = "application/octet-stream";
/// <summary>
/// 临时文件夹
/// </summary>
//public readonly static string TempFileFolder = ConfigurationManager.AppSettings["TempFileFolder"];
public readonly static string TempFileFolder = @"c:cmis emp";
/// <summary>
/// 附件上载时使用
/// </summary>
public readonly static string AttachmentSavedFolder = @"c:cmisdoc";
/// <summary>
/// 默认的数据Provider名称
/// </summary>
public readonly static string CurrentProviderName = "CMISDOCDATA";
static void Main()
{
Console.WriteLine("===== 开始下载文件 =====");
GetDownload(Guid.NewGuid(), "https://images.cnblogs.com/cnblogs_com/storebook/1356555/o_abc.jpg");
Console.WriteLine("===== 完成下载文件 =====");
Console.ReadKey();
}
#region 根据URI把资源保存到本地
/// <summary>
/// 根据URI把资源保存到本地
/// </summary>
/// <param name="id"></param>
/// <param name="uri"></param>
public static void GetDownload(Guid id, string uri)
{
Directory.CreateDirectory(TempFileFolder);
string path = string.Empty;
EnumAttachmentStatus status = EnumAttachmentStatus.Fail;
string result = string.Empty;
#region 1.根据uri把文件存到本地
try
{
string filename = id.ToString() + uri.Substring(uri.LastIndexOf('.'));
path = string.Format(@"{0}{1}", TempFileFolder, filename);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responseStream = response.GetResponseStream();
if (response.ContentType == "text/html")
{
status = EnumAttachmentStatus.Fail;
result = "[下载失败]资源无效。";
}
else
{
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, bArr.Length);
}
stream.Close();
responseStream.Close();
status = EnumAttachmentStatus.OK;
result = "下载成功。";
}
}
catch (Exception e)
{
status = EnumAttachmentStatus.Fail;
result = "[下载失败-异常]" + e.Message;
}
#endregion
string extension = System.IO.Path.GetExtension(path);
Directory.CreateDirectory(AttachmentSavedFolder);
string savedFileName = string.Format("{0}_{1}_{2}_{4}{3}"
, id
, System.IO.Path.GetRandomFileName()
, DateTime.Now.ToString("yyyyMMdd_hh_mm")
, extension
, CurrentProviderName);
string destFileName = System.IO.Path.Combine(AttachmentSavedFolder, savedFileName);
#region 2.移动文件
if (status == EnumAttachmentStatus.OK)
{
try
{
File.Move(path, destFileName);
status = EnumAttachmentStatus.OK;
result = "[Move]文件上传成功。";
}
catch (Exception e)
{
status = EnumAttachmentStatus.Fail;
result = "[Move-文件上传失败-异常]" + e.Message;
}
}
#endregion
long filesize = 0; //
string folderName = path.Substring(path.LastIndexOf('\') + 1);
string displayFileName = "";
#region 3.把文件存到AM_AttachmentInfo(CMISDOCDATA)表
if (status == EnumAttachmentStatus.OK)
{
Guid attachmentID = Guid.NewGuid();
}
#endregion
#region 4. 保存下载记录
#endregion
}
#endregion
}
#region 附件状态
/// <summary>
/// 附件状态
/// </summary>
public enum EnumAttachmentStatus
{
/// <summary>
/// 未处理
/// </summary>
Untreated = 0,
/// <summary>
/// 成功
/// </summary>
OK = 1,
/// <summary>
/// 失败
/// </summary>
Fail = 2,
}
#endregion
}