C#下载文件的方法包含本地下载,http下载,以及返回字节流方法

#region 调用本地文件使用返回pdfbyte数组

        /// 
        /// 调用本地文件使用返回pdfbyte数组
        /// 
        /// ‘D:\in2434341555551.pdf’
        /// 

        public static Byte[] GetSignaturePDFByte(string srcPdfFile)
        {
            using (FileStream fsRead = new FileStream(srcPdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                int fsLen = (int)fsRead.Length;
                byte[] hebyte = new byte[fsLen];
                fsRead.Read(hebyte, 0, hebyte.Length);
                return hebyte;
            }
        }

        #endregion 调用本地文件使用返回pdfbyte数组

        #region 从网站上下载pdf,转化为字节流


        /// 
        /// 从网站上下载pdf,转化为字节流
        /// 
        /// 文件地址:'https://******/group2/M00/00/04/wKj-mlpcoZ2IUbK5AACrpaV6k98AAAB6gAAAAAAAKu9562.pdf'

        /// 
        public static Byte[] GetByteByRemoteURL(string srcPdfFile)
        {
            byte[] arraryByte;
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(srcPdfFile);
            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                StreamReader responseStream = new StreamReader(wr.GetResponseStream(), Encoding.UTF8);
                int length = (int)wr.ContentLength;
                byte[] bs = new byte[length];

                HttpWebResponse response = wr as HttpWebResponse;
                Stream stream = response.GetResponseStream();

                //读取到内存
                MemoryStream stmMemory = new MemoryStream();
                byte[] buffer1 = new byte[length];
                int i;
                //将字节逐个放入到Byte 中
                while ((i = stream.Read(buffer1, 0, buffer1.Length)) > 0)
                {
                    stmMemory.Write(buffer1, 0, i);
                }
                arraryByte = stmMemory.ToArray();
                stmMemory.Close();
            }
            return arraryByte;
        }

        #endregion 从网站上下载pdf,转化为字节流

        #region 从网站上下载文件,保存到其他路径

        /// 
        /// 从网站上下载文件,保存到其他路径
        /// 
        /// 文件地址
        /// 保存文件路径:D:\12221.pdf
        /// 
        public static bool SaveRemoteFile(string pdfFile, string saveLoadFile)
        {
            bool flag = false;

            Uri downUri = new Uri(pdfFile);
            //建立一个WEB请求,返回HttpWebRequest对象
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(downUri);
            //流对象使用完后自动关闭
            using (Stream stream = hwr.GetResponse().GetResponseStream())
            {
                //文件流,流信息读到文件流中,读完关闭
                using (FileStream fs = File.Create(saveLoadFile))
                {
                    //建立字节组,并设置它的大小是多少字节
                    byte[] bytes = new byte[102400];
                    int n = 1;
                    while (n > 0)
                    {
                        //一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
                        n = stream.Read(bytes, 0, 10240);
                        fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
                    }
                }
            }

            return flag;
        }

        #endregion 从网站上下载文件,保存到其他路径

 

你可能感兴趣的:(c#)