榕树下。。。

在成功的道路上,你没有耐心去等待成功的到来,那么,你只好用一生的耐心去面对失败
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

超级郁闷之问题,请DUDU及各位大位指正错误

Posted on 2006-05-26 17:16  农村的芬芳  阅读(708)  评论(1编辑  收藏  举报

事情皆因一文件下载惹的祸。首先我介绍我的机器构成:文件服务器,暂叫A服务器,也就是放文件的机器。应用程序服务器,这大家都明白,就是部署程序的服务器,其它服务器就不说啦。
好,开始说问题,最初小弟,根本没去想太多的性能与服务器的承载量的问题。就写了下面两段代码

string physicalFilePath = downloadpath;
                FileStream stream
=null;
                
try 
                
{
                    stream 
= new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);    
                    
int bufSize = (int)stream.Length;
                    
byte[] buf = new byte[bufSize];

                    
int bytesRead = stream.Read(buf, 0, bufSize);
                    HttpContext.Current.Response.ContentType 
= "application/octet-stream"
                    HttpContext.Current.Response.AppendHeader(
"Content-Disposition""attachment;filename="+Server.UrlEncode(System.IO.Path.GetFileName(Server.UrlEncode(filename))));
                    HttpContext.Current.Response.OutputStream.Write(buf, 
0, bytesRead);

                    

                    HttpContext.Current.Response.End();
                }

                
finally 
                
{
                    stream.Close();
                }



或:
downloadpath为变量
System.IO.FileInfo file 
= new System.IO.FileInfo(downloadpath);
                    Response.Clear();
                    Response.Charset
="GB2312";
                    Response.ContentEncoding
=System.Text.Encoding.UTF8;
            
                    Response.AddHeader(
"Content-Disposition""attachment; filename="+Server.UrlEncode(filename));
                    Response.AddHeader(
"Content-Length", file.Length.ToString());

                                    
                    Response.ContentType 
= "application/ms-excel";
                    Response.WriteFile(file.FullName);
Response.End();
刚开始放上去时,根本没有任何问题,因为访问量与数据文件都不够大。
等过几天,访问量达到几千人同时在线时,我的问题来啦,有的数据文件也比较大,有的是100M以上的,哪上面这两种方法同时挂掉,有的因为文件太大,而无法输出,有的的有时莫明其妙的弹出服务器重置这种提示,后来我又换啦一种下载方式,代码如下:
这是从网上看到一位同仁写的,就拿过来一试,发现对大文件下载,还真不错,能顺利下载,好一段时间都没问题,心想这下可好啦,但没成想,好日子没几天,问题又接二连三的回来啦,这次的问题更莫明其妙,有的是下载到99%时,突然提示不能下载,有的呢,却一点击下载,弹出框后,就一点进度都还没有时,就一会儿弹出来一个连接超时,服务器重置的提示,把我给郁闷的,但同时呢,其它文件又可以下载用同一段代码。郁闷中ing....希望有哪位能指点一下。
文件肯定存在,链接肯定正确