using COMMON; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace SERVER { public class FTPOperation { /// /// 从FTP中获取文件 /// /// 路径 /// public byte[] GetFile(string url) { //Logs.WriteLog("222获取图片开始"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")); url = "ftp://auth.blv-oa.com:50/BLV_Studio/Data/"+ url; FtpWebRequest ftpWeb; try { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); try { ftpWeb = (FtpWebRequest)FtpWebRequest.Create(url);//创建ftp连接 ftpWeb.Credentials = new NetworkCredential("uts_manager", "uts_Inhaos@all");//账号,密码 ftpWeb.KeepAlive = false; ftpWeb.EnableSsl = false; ftpWeb.Method = WebRequestMethods.Ftp.DownloadFile; ftpWeb.UseBinary = true; ftpWeb.UsePassive = true; WebResponse ftpResponse = (FtpWebResponse)ftpWeb.GetResponse(); Stream ftpStream = ftpResponse.GetResponseStream();//获取流 MemoryStream outstream = new MemoryStream();//转成MS流 int bufferLen = 4096;//缓冲区 byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = ftpStream.Read(buffer, 0, bufferLen)) > 0) { outstream.Write(buffer, 0, count); } var FileLiu = outstream.ToArray(); ftpResponse.Close(); ftpStream.Close(); outstream.Close(); return FileLiu; //return BytesToStream(FileLiu); } catch (Exception e) { return null; } } catch (Exception ex) { return null; } } /// 将 byte[] 转成 Stream public Stream BytesToStream(byte[] bytes) { Stream stream = new MemoryStream(bytes); return stream; } public string UnHexs(string hex) { if (hex == null) throw new ArgumentNullException("hex"); hex = hex.Replace(",", ""); hex = hex.Replace("\n", ""); hex = hex.Replace("\\", ""); hex = hex.Replace(" ", ""); if (hex.Length % 2 != 0) { hex += "20";//空格 } // 需要将 hex 转换成 byte 数组。 byte[] bytes = new byte[hex.Length / 2]; for (int i = 0; i < bytes.Length; i++) { try { // 每两个字符是一个 byte。 bytes[i] = byte.Parse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); } catch { // Rethrow an exception with custom message. throw new ArgumentException("hex is not a valid hex number!", "hex"); } } System.Text.Encoding chs = System.Text.Encoding.GetEncoding("UTF-8"); return chs.GetString(bytes); } /// /// 字节数组转换成十六进制字符串 /// /// 要转换的字节数组 /// public string ByteArrayToHexStr(byte[] byteArray) { int capacity = byteArray.Length * 2; StringBuilder sb = new StringBuilder(capacity); if (byteArray != null) { for (int i = 0; i < byteArray.Length; i++) { sb.Append(byteArray[i].ToString("X2")); } } return sb.ToString(); } /// /// 无证书时跳过不报错 /// /// /// /// /// /// public static bool ValidateServerCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; } } }