博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取程序所在文件路径及保存STREAM到本地文件
阅读量:2386 次
发布时间:2019-05-10

本文共 2445 字,大约阅读时间需要 8 分钟。

自定义一个方法

public static string MapPath(string strPath)
  {
  if (System.Web.HttpContext.Current != null)
  {
  return System.Web.HttpContext.Current.Server.MapPath(strPath);
  }
  else //非web程序引用
  {
  strPath = strPath.Replace("/", "");
  strPath = strPath.Replace("~", "");
  if (strPath.StartsWith("\\"))
  {
  strPath = strPath.TrimStart('\\');
  }
  return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
  }
  }

 

-------------------------------------------------------------------------------------------------------------------------------

 

   private HttpWebRequest CreateRequest(string path, string queryString = null)

        {
            if (!string.IsNullOrWhiteSpace(queryString) && !queryString.StartsWith("?"))
                queryString = "?" + queryString;

            var url = new Uri(BaseUrl, path + queryString).AbsoluteUri;

            var request = (HttpWebRequest)WebRequest.Create(url);

            request.Headers["Authorization"] = AuthorizationKey;

            request.Timeout = TimeoutSeconds == System.Threading.Timeout.Infinite ? TimeoutSeconds : TimeoutSeconds * 1000;

            return request;

        }

        public bool SaveResponseContentToCSV(String path, String queryString = null)

        {
            var request = CreateRequest(path, queryString);
            var requestTrace = string.Empty;

            using (var response = (HttpWebResponse)request.GetResponse())

            {
                try
                {
                    var fileType = response.ContentType;
                    var index = response.Headers["Content-Disposition"].IndexOf("=") + 1;
                    var fileName = response.Headers["Content-Disposition"].Substring(index);

                    Stream respStream = response.GetResponseStream();

                    int length = (int)response.ContentLength;
                    BinaryReader br = new BinaryReader(respStream);
                    FileStream fs;
                    //var localFilePath = System.Web.HttpContext.Current.Server.MapPath("\\dailyClickActivity" + "\\" + System.DateTime.Now.ToLongTimeString());
                    //AppDomain.CurrentDomain.BaseDirectory
                    var localFilePath = System.IO.Path.Combine(Environment.CurrentDirectory, "/dailyClickActivity" + "/" + System.DateTime.Now.ToString("yyyyMMdd"));
                    if (!Directory.Exists(localFilePath))
                        Directory.CreateDirectory(localFilePath);

                    fs = File.Create(string.Format("{0}/{1}", localFilePath, fileName));

                    fs.Write(br.ReadBytes(length), 0, length);

                    br.Close();

                    fs.Close(); 

                    return true;

                }
                catch (Exception ex)
                {
                    requestTrace += ex.Message;
                    throw;
                }
                finally
                {
                    //TracerLogger.Log(requestTrace, EventLogEntryType.Information, Constants.RoleNameAdFabricClick);
                    Console.WriteLine(string.Format("{0},{1},{2}", requestTrace, EventLogEntryType.Information, Constants.RoleNameAdFabricClick));

                }

            }
        }

 

转载地址:http://qojab.baihongyu.com/

你可能感兴趣的文章
冒号和他的学生们(连载10)——超级范式
查看>>
冒号和他的学生们(连载9)——泛型范式
查看>>
冒号和他的学生们(连载13)——范式总结
查看>>
A Proposal on Organization of Information System
查看>>
冒号和他的学生们(连载2)——首轮提问
查看>>
正则表达式与文件格式化处理
查看>>
Java EE互联网轻量级框架整合开发
查看>>
Java语言程序设计(基础篇)
查看>>
大型网站技术架构:核心原理与案例分析
查看>>
JAVA并发编程实战
查看>>
RabbitMQ实战++高效部署分布式消息队列
查看>>
微服务设计
查看>>
Spring Cloud微服务实战
查看>>
C++ static 语义
查看>>
C++ static 语义
查看>>
Linux Cgroups概述
查看>>
centos7 硬盘性能测试
查看>>
cgroup使用--cpu资源限制
查看>>
cgroup使用--memory资源限制
查看>>
Redis 单机环境搭建
查看>>