首页 > Web开发 > 详细

下载文件 http路径ftp路径

时间:2020-08-27 18:32:13      阅读:116      评论:0      收藏:0      [点我收藏+]
 /// <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路径

原文:https://www.cnblogs.com/macT/p/13572573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!