• stream流写到MemoryStream内存流引发得问题


    采用httphandler去中转访问认证服务器。以下代码实现得是从认证远程服务器上下载资源到本地客户端。但实际连接到远程服务器上,下载文件所花费得时间非常长。大家分析下代码?看看程序所执行得时间花费在哪里,并减少这个时间花费。我认为该程序所花费太多时间,是发生在stream流写到内存流上。谢谢。讨论讨论。

    页面上调用方法:

    <a href="/aklib.aspx?fn=文件完整路径" target="_blank">文件名</a>

    code:


    public class AutoDownload : IHttpHandler
    {

    #region IHttpHandler Members

    public bool IsReusable
    {
    get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
    try
    {
    string fileName = SoFContext.Current.GetStringInQs("fn", string.Empty);
    string siteUrl= System.Configuration.ConfigurationManager.AppSettings["SiteUrl"].ToString();
    string user = System.Configuration.ConfigurationManager.AppSettings["FtpUser"].ToString();
    string passWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();

    string filePath = string.Empty;
    filePath
    = siteUrl+ fileName;

    string file = Path.GetFileName(filePath);

    HttpContext.Current.Response.Buffer
    = true;
    HttpContext.Current.Response.Charset
    = "utf-8";
    HttpContext.Current.Response.ContentEncoding
    = Encoding.UTF8;
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType
    = "application/octet-stream";
    HttpContext.Current.Response.AppendHeader(
    "Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file, Encoding.UTF8));

    HttpWebRequest httpWebRequest;
    httpWebRequest
    = (HttpWebRequest)HttpWebRequest.Create(filePath);

    NetworkCredential credential;
    if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(passWord))
    {
    credential
    = new NetworkCredential(user, passWord);
    httpWebRequest.Credentials
    = credential;
    }

    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
    {
    using (Stream responseStream = httpWebResponse.GetResponseStream())
    {

    byte[] arrayByte;
    byte[] buffer = new byte[4096];
    using (MemoryStream stmMemory = new MemoryStream())
    {

    int count = 0;
    do
    {
    count
    = responseStream.Read(buffer, 0, buffer.Length);
    stmMemory.Write(buffer,
    0, count);

    }
    while (count != 0);

    arrayByte
    = stmMemory.ToArray();

    HttpContext.Current.Response.AppendHeader(
    "Content-Length", arrayByte.Length.ToString());
    HttpContext.Current.Response.BinaryWrite(arrayByte);

    HttpContext.Current.Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    }



    }
    }
    }
    catch (Exception ex)
    {
    System.Diagnostics.Debug.Fail(ex.Message);
    System.Diagnostics.Debug.Fail(ex.StackTrace);

    }
    }

    HttpHandler有时使用并不是拥有很的性能。

    考虑流量大的问题:建议使用异步的HttpHandler(具体参考MSDN做法)

    那你做的方法:确

    你的 HttpWebRequest是同步的, 如果文件大+并发高了 结果你自己知道!

    另外你通过这种方式下载,相当于下了2次! 你首先下载了一次到服务器内存,然后再把内存的东西发给用户.

    实是拿内存进行了中转,所以花费一些时间。

     
  • 相关阅读:
    子类调用父类被重写的方法
    Linux下编译出现undefined reference to ‘pthread_create’问题解决
    CRC校验8
    嵌入式C语言查表法
    Static关键字,遇到的问题_1
    java中方法的参数传递机制_一个对象被当作参数传递到一个方法后
    String使用equals和==比较的区别
    如何导入XML数据 (python3.6.6区别于python2 环境)
    0xx_PHP核心01
    PHP_MVC设计模式02
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/1651617.html
Copyright © 2020-2023  润新知