隐藏

C#远程传输(二进制流)文件及判断远程文件是否存在

发布:2014/7/23 21:57:32作者:管理员 来源:本站 浏览次数:1638

///1.判断远程文件是否存在 

///fileUrl:远程文件路径,包括IP地址以及详细的路径

private bool RemoteFileExists(string fileUrl)
        {
            bool result = false;//下载结果

            WebResponse response = null;
            try
            {
                WebRequest req = WebRequest.Create(fileUrl);

                response = req.GetResponse();

                result = response == null ? false : true;

            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }

            return result;
        }

 

 

 

        

         /// 2.通过传入的url获取远程文件数据(二进制流),大家可以通过Response.BinaryWrite()方法将获取的数据流输出

        /// </summary>
        /// <param name="url">图片的URL</param>
        /// <param name="ProxyServer">代理服务器</param>
        /// <returns>图片内容</returns>
        public byte[] GetFile(string url, string proxyServer)
        {
            WebResponse rsp = null;
            byte[] retBytes = null;

            try
            {
                Uri uri = new Uri(url);
                WebRequest req = WebRequest.Create(uri);

                rsp = req.GetResponse();
                Stream stream = rsp.GetResponseStream();

                if (!string.IsNullOrEmpty(proxyServer))
                {
                    req.Proxy = new WebProxy(proxyServer);
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    int b;
                    while ((b = stream.ReadByte()) != -1)
                    {
                        ms.WriteByte((byte)b);
                    }
                    retBytes = ms.ToArray();
                }
            }
            catch (Exception ex)
            {
                retBytes = null;
            }
            finally
            {
                if (rsp != null)
                {
                    rsp.Close();
                }

 

 

          ///将本地文件通过httpwebrequest上传到服务器

          ///localFile:本地文件路径及文件名称,uploadUrl:远程路径(虚拟目录)

          ///在远程路径中需要把文件保存在的文件夹打开权限设置,否则上传会失败

         public bool UploadFileBinary(string localFile, string uploadUrl)
        {
            bool result = false;
            HttpWebRequest req = null;
            Stream reqStream = null;
            FileStream rdr = null;

            try
            {

                req = (HttpWebRequest)WebRequest.Create(uploadUrl);

                req.Method = "PUT";
                req.AllowWriteStreamBuffering = true;

                // Retrieve request stream 
                reqStream = req.GetRequestStream();


                // Open the local file
                rdr = new FileStream(localFile, FileMode.Open);

                byte[] inData = new byte[4096];

                int bytesRead = rdr.Read(inData, 0, inData.Length);
                while (bytesRead > 0)
                {
                    reqStream.Write(inData, 0, bytesRead);
                    bytesRead = rdr.Read(inData, 0, inData.Length);
                }

                rdr.Close();
                reqStream.Close();

                req.GetResponse();

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            finally
            {
                if (reqStream != null)
                {
                    reqStream.Close();
                }
                if (rdr != null)
                {
                    rdr.Close();
                }
            }
            return result;
        }


            }
            return retBytes;
        }