using RiaService.Common; using System; using System.Configuration; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.Http; using WX.CRM.Common; namespace RiaService { public class CallRecordController : ApiController { public string GetCallFile(string serverID, string file) { if (string.IsNullOrEmpty(serverID) || string.IsNullOrEmpty(file)) { return null; } RecordServerConfig configUtil = ConfigurationManager.GetSection("RecordServer") as RecordServerConfig; if (configUtil == null) { LogHelper.Error("CallRecord.GetCallFile错误,web.config录音服务器节错误"); return null; } var server = configUtil.ServerList.GetServerList().FirstOrDefault(obj => obj.ServerID == serverID); if (null == server) { LogHelper.Error("CallRecord.GetCallFile错误,录音服务器:" + serverID + "不存在"); return null; } string localurl = "..//CallRecordFile//" + file; localurl = HttpContext.Current.Server.MapPath(localurl); if (ChkRemoteFileExists(localurl))//文件已经存在 { return localurl; } else { string remoteUri = server.Remote + server.From + "\\" + file; string d = localurl.Substring(0, localurl.LastIndexOf("\\", System.StringComparison.Ordinal)); if (!Directory.Exists(d)) { Directory.CreateDirectory(d); } try { int status = NetworkConnection.Connect(server.Remote + server.From, "Z:", server.UserName, server.Password); if (status == (int)ERROR_ID.ERROR_SUCCESS) { if (!File.Exists(localurl)) { NetworkConnection.CopyFiles(remoteUri, localurl); if (!File.Exists(localurl)) { remoteUri = server.Remote + server.To + "\\" + file; NetworkConnection.CopyFiles(remoteUri, localurl); } } } else { LogHelper.Error("连接服务器失败:" + server.Remote + server.From); } NetworkConnection.Disconnect("Z:"); return localurl; } catch (Exception ex) { LogHelper.Error(ex); return null; } } } //判断远程文件是否存在 private bool ChkRemoteFileExists(string fileUrl) { bool result = false;//判断结果 WebResponse response = null; try { WebRequest req = WebRequest.Create(fileUrl); response = req.GetResponse(); result = true; } catch (Exception ex) { result = false; } finally { if (response != null) { response.Close(); } } return result; } } }