服务器下载文件http

    #region 从服务器下载文件
    /// <summary>
    /// 从服务器下载文件
    /// </summary>
    /// <param name="_Request"></param>
    /// <param name="_Response"></param>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_fullPath">文件完整路径</param>
    /// <param name="_speed">速度</param>
    /// <returns></returns>
    public bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
    {
        try
        {
            FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            BinaryReader br = new BinaryReader(myFile);
            try
            {
                _Response.AddHeader("Accept-Ranges", "bytes");
                _Response.Buffer = false;
                long fileLength = myFile.Length;
                long startBytes = 0;

                int pack = 10240; //10K bytes
                //int sleep = 200;   //每秒5次   即5*10K bytes每秒
                int sleep = (int)Math.Floor((decimal)1000 * pack / _speed) + 1;
                if (_Request.Headers["Range"] != null)
                {
                    _Response.StatusCode = 206;
                    string[] range = _Request.Headers["Range"].Split(new char[] { ‘=‘, ‘-‘ });
                    startBytes = Convert.ToInt64(range[1]);
                }
                _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                if (startBytes != 0)
                {
                    _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                }
                _Response.AddHeader("Connection", "Keep-Alive");
                _Response.ContentType = "application/octet-stream";
                _Response.Charset = "UTF-8";
                _Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
                _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

                br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                int maxCount = (int)Math.Floor((decimal)(fileLength - startBytes) / pack) + 1;

                for (int i = 0; i < maxCount; i++)
                {
                    if (_Response.IsClientConnected)
                    {
                        _Response.BinaryWrite(br.ReadBytes(pack));
                        Thread.Sleep(sleep);
                    }
                    else
                    {
                        i = maxCount;
                    }
                }
                _Response.End();

            }
            catch
            {
                return false;
            }
            finally
            {
                br.Close();
                myFile.Close();
                //下载后后删除路径

              /*
                if (File.Exists(_fullPath))
                {
                    File.Delete(_fullPath);
                }

              */
            }
        }
        catch
        {
            return false;
        }

        return true;

    }
    #endregion

服务器下载文件http

时间: 2024-08-01 12:03:48

服务器下载文件http的相关文章

wp8通过WebClient从服务器下载文件

通过WebClient从Web服务器下载文件,并保存到wp8手机应用程序的独立存储. 我们可以通过利用webClient_DownloadStringCompleted来获得下载完成所需要的时间,用Stopwatch得到下载的总时间. 通常我们都将上传.下载作为异步事件来处理,以便不阻止主线程. String url = "http://172.18.144.248:8080/upload/" + filename; WebClient client = new WebClient()

本地上传文件到服务器,从服务器下载文件到本地

最近在做项目的时候涉及到了文件的上传.下载,以前学习IO时也没有搞得多清楚,在网上找了些上传下载的例子,然后修改了部分.经测试,上传下载文件暂时能用,下面是上传和下载的方法: 1.本地上传文件到服务器 html代码: <form id="uploadDatumInfo" name="uploadDatumInfo" method="post" enctype="multipart/form-data" target=&q

C# 实现访问FTP服务器下载文件,获取文件夹信息小记

最近因为要开发广告制作工具,自动生成广告流,需要获取第三方服务器上的文件资源,经过摸索,从这次经历中记录下. FtpWebRequest reqFtp; WebResponse response = null; //获取文件夹信息 reqFtp = (FtpWebRequest)WebRequest.Create(this.ftp);//ftp://IP:port/文件夹名1/文件夹名2/.../文件夹名 reqFtp.UseBinary = true; reqFtp.KeepAlive = f

putty windows上传文件到linux服务器 &amp; 从linux服务器 下载文件到 windows

从putty官网下载putty软件:putty.exepscp.exepsftp.exe等软件 也可以自己下windows安装包putty-0.63-installer.exe 本人直接下载putty-0.63-installer.exe安装包了,然后直接安装 使用pscp方式从windows上传文件到linux服务器 在CMD命令行中进入到putty安装目录 输入pscp 回车 pscp 跟我们平时使用的linux scp命令操作的都是类似的 现在我直接从windows本地上传一个文件到lin

scp从服务器下载文件或上传文件到服务器

1.从服务器下载文件: scp [email protected]:/xxx/xxx.xxx e:/test/ 2.从本地上传文件到服务器 scp e:/test/test.log [email protected]:/text 理解: scp 操作的文件地址    要上传的文件地址 从服务器下载文件就好比从服务器上传文件到本地服务器 原文地址:https://www.cnblogs.com/itwlp/p/10859476.html

从Linux服务器下载文件夹到本地

从Linux服务器下载文件夹到本地1.使用scp命令 scp /home/work/source.txt [email protected]:/home/work/   #把本地的source.txt文件拷贝到192.168.0.10机器上的/home/work目录下 scp [email protected]:/home/work/source.txt /home/work/   #把192.168.0.10机器上的source.txt文件拷贝到本地的/home/work目录下 scp [em

Java Web实现使用浏览器从服务器下载文件(后台)

Java Web实现 使用浏览器从服务器下载文件. 下面实现两种情况的下载,需求如下: 需求(一):1.用户在页面填写表单. 2.填写完成后,选择下载,将表单内容发往后台. 3.后台根据内容生产一个文件,发送给前端. 4.前端成功下载文件到本地. 此需求简单来说就是,用户在页面上填写内容,然后将内容转变成文件的形式. 后台设计思路:1.首先拿到前端发送过来的内容. 2.将内容解析,存放至缓冲区. 3.设置响应头. 4.将缓冲区里的内容,以流的方式写出. 代码实现: public void dow

通过pscp从linux服务器下载文件到本地windows操作系统记载

从linux 服务器下载数据到本地windows操作系统 1) 下载 putty.exe 完整安装包. 2) 将pscp.exe程序 放在C:/windows/system32文件下(或者配置pscp.e xe的系统变量). 3)   打开windows cmd.exe命令行窗口 输入pscp命令看是否能够运行. 4)   在命令行运行pscp [email protected]:/usr/tideway/java/integrations/mappings/extended-rdb/Oracl

C#实现开发windows服务实现自动从FTP服务器下载文件(自行设置分/时执行)

最近在做一个每天定点从FTP自动下载节目.xml并更新到数据库的功能.首先想到用 FileSystemWatcher来监控下载到某个目录中的文件是否发生改变,如果改变就执行相应的操作,然后用timer来设置隔多长时间来下载.后来又想想了.用windwos服务来实现吧. 效果图: 执行的Log日志: INFO-2016/5/24 0:30:07--日志内容为:0/30/7进行time触发 INFO-2016/5/24 1:30:07--日志内容为:1/30/7进行time触发 INFO-2016/