在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
///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; } 本文转载于:http://www.cnblogs.com/tsgx108/archive/2008/11/18/1335618.html感谢原作者的贡献。转载请著名出处
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论