zoukankan      html  css  js  c++  java
  • Code-C#:根据URI把资源保存到本地

    ylbtech-Code-C#:根据URI把资源保存到本地
    1.返回顶部
    1、
    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
    }
    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
     
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Java8学习笔记(五)--Stream API详解[转]
    Java8学习笔记(四)--接口增强
    Java8学习笔记(三)--方法引入
    JAVA8学习笔记(二)----三个预定义接口
    JAVA8学习笔记(一)----Lambda表达式
    Java基础加强总结(三)——代理(Proxy)
    Java基础加强总结(二)——泛型
    mysql统计表的大小
    jquery异步上传图片
    瀑布流
  • 原文地址:https://www.cnblogs.com/storebook/p/12442156.html
Copyright © 2011-2022 走看看