下载文件 http路径ftp路径

 /// <summary>
        /// 下载文件 http路径ftp路径
        /// </summary>
        /// <param name="uri">文件地址</param>
        /// <returns>字节数组</returns>
        static private byte[] ConvertFileToByte(string uri)
        {
            byte[] by = null;
            if (!string.IsNullOrEmpty(uri))
            {
                WebRequest webRequest = null;
                WebResponse webResponse = null;
                try
                {
                    if (uri.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)
                   || uri.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))
                    {
                        webRequest = (HttpWebRequest)WebRequest.Create(uri);
                        webRequest.Method = "get";
                        webRequest.Proxy = null;
                        webResponse = (HttpWebResponse)webRequest.GetResponse();
                    }
                    else if (uri.StartsWith("ftp://", StringComparison.CurrentCultureIgnoreCase))
                    {
                        webRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                        webRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                        ((FtpWebRequest)webRequest).UseBinary = true;
                        webRequest.Proxy = null;
                        webResponse = (FtpWebResponse)webRequest.GetResponse();
                    }
                    else
                    {
                        throw new ArgumentException("uri非http ftp地址");
                    }
                    Stream responseStream = webResponse.GetResponseStream();
                    //创建本地文件写入流
                    MemoryStream stream = new MemoryStream();
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, bArr.Length);
                    while (size > 0)
                    {
                        stream.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, bArr.Length);
                    }
                    by = stream.ToArray();
                    stream.Close();
                    responseStream.Close();
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    if (webRequest != null)
                    {
                        webRequest.Abort();
                    }
                    if (webResponse != null)
                    {
                        webResponse.Close();
                    }
                }
            }
            else 
            {
                throw new ArgumentException("参数uri空异常");
            }          
            return by;
        }

        /// <summary>
        /// 下载文件 http路径ftp路径
        /// </summary>
        /// <param name="uri">文件地址</param>
        /// <returns>字节数组</returns>
        public static byte[] DownLoadFile(string uri)
        {
            byte[] bytes = null;
            if (!string.IsNullOrEmpty(uri))
            {
                WebRequest webRequest = null;
                WebResponse webResponse = null;
                try
                {
                    if (uri.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)
                || uri.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase))
                    {

                        webRequest = (HttpWebRequest)WebRequest.Create(uri);
                        webRequest.Method = "get";
                        webRequest.Proxy = null;
                        webResponse = (HttpWebResponse)webRequest.GetResponse();
                    }
                    else if (uri.StartsWith("ftp://", StringComparison.CurrentCultureIgnoreCase))
                    {
                        webRequest = (FtpWebRequest)WebRequest.Create(new Uri(uri));
                        webRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                        ((FtpWebRequest)webRequest).UseBinary = true;
                        webRequest.Proxy = null;
                        webResponse = (FtpWebResponse)webRequest.GetResponse();
                    }
                    else
                    {
                        throw new ArgumentException("uri非http ftp地址");
                    }

                    using (webResponse)
                    {
                        using (Stream responseStream = webResponse.GetResponseStream())
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                byte[] buffer = new byte[2048];
                                int read = 0;
                                do
                                {
                                    read = responseStream.Read(buffer, 0, buffer.Length);
                                    ms.Write(buffer, 0, read);
                                    ms.Flush();
                                } while (read != 0);
                                ms.Flush();
                                bytes = ms.ToArray();
                            }
                        }
                    }
                }
                finally
                {
                    if (webRequest != null)
                    {
                        webRequest.Abort();
                    }
                    if (webResponse != null)
                    {
                        webResponse.Close();
                    }
                }
            }
            else
            {
                throw new ArgumentException("参数uri空异常");
            }
            return bytes;
        }

 

下载文件 http路径ftp路径

上一篇:Vue vue.config.js 的详解与配置 (十五)


下一篇:vue项目更换网站title旁边的小图标