zoukankan      html  css  js  c++  java
  • c# 通过地址下载流然后保存文件到本地

    1.下载文件并保存文件到本地

            private void GetFileFromNetUrl(string url)
            {
                try
                {
                    System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                    req.Method = "GET";
                    //获得用户名密码的Base64编码  添加Authorization到HTTP头 不需要的账号密码的可以注释下面两行代码
                    string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "userName", "passWord")));
                    req.Headers.Add("Authorization", "Basic " + code);
                    byte[] fileBytes;
                    using (WebResponse webRes = req.GetResponse())
                    {
                        int length = (int)webRes.ContentLength;
                        HttpWebResponse response = webRes as HttpWebResponse;
                        Stream stream = response.GetResponseStream();
    
                        //读取到内存
                        MemoryStream stmMemory = new MemoryStream();
                        byte[] buffer = new byte[length];
                        int i;
                        //将字节逐个放入到Byte中
                        while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stmMemory.Write(buffer, 0, i);
                        }
                        fileBytes = stmMemory.ToArray();//文件流Byte,需要文件流可直接return,不需要下面的保存代码
                        stmMemory.Close();
    
                        MemoryStream m = new MemoryStream(fileBytes);
                        string file = string.Format("F:\666666666666.pdf");//可根据文件类型自定义后缀
                        FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
                        m.WriteTo(fs);
                        m.Close();
                        fs.Close();
                    }
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }      

    2.下载文件流,返回byte流

            private byte[] GetFileFromNetUrl(string url, string userName = "", string password = "")
            {
                try
                {
                    System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                    req.Method = "GET";
                    if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
                    {
                        //获得用户名密码的Base64编码  添加Authorization到HTTP头 
                        string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password)));
                        req.Headers.Add("Authorization", "Basic " + code);
                    }
    
                    byte[] fileBytes;
                    using (WebResponse webRes = req.GetResponse())
                    {
                        int length = (int)webRes.ContentLength;
                        HttpWebResponse response = webRes as HttpWebResponse;
                        Stream stream = response.GetResponseStream();
    
                        //读取到内存
                        MemoryStream stmMemory = new MemoryStream();
                        byte[] buffer = new byte[length];
                        int i;
                        //将字节逐个放入到Byte中
                        while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            stmMemory.Write(buffer, 0, i);
                        }
                        fileBytes = stmMemory.ToArray();//文件流Byte
                        stmMemory.Close();
                    }
                    return fileBytes;
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
    愿你我既可以朝九晚五,也可以浪迹天涯;愿你我既可以拈花把酒,也能围炉诗书茶。
  • 相关阅读:
    如何利用python爬虫爬取音乐
    python中文件操作各符号意思
    基于node.js人脸识别之人脸对比
    体感在js中的调用
    了解Github
    初识微信小程序
    Spring中表达式语言spring-expression简单使用
    IDEA对jsr305的Nonnull注解和Guava的Beta注解的支持
    设计模式之访问者模式
    设计模式之桥接模式
  • 原文地址:https://www.cnblogs.com/xiaoheihei/p/10076973.html
Copyright © 2011-2022 走看看