Bladeren bron

增加 上传语音文件并转换格式 接口

machenyang 8 jaren geleden
bovenliggende
commit
4d562d5bd5

+ 7 - 0
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Configs/system.config

@@ -25,4 +25,11 @@
25 25
   <add key="WechatAppid" value="wx957c2297c315e6a5" />
26 26
   <!-- 设置微信appsecret -->
27 27
   <add key="WechatAppsecret" value="6c2d1f9fd32f906c315f03398800d474" />
28
+  <!-- ================== 4:IVR文件上传FTP配置参数 ================== -->
29
+  <!-- 设置FTP -->
30
+  <add key="ftp" value="192.168.1.30" />
31
+  <!-- 设置账户名 -->
32
+  <add key="account" value="ceshi" />
33
+  <!-- 设置密码 -->
34
+  <add key="password" value="123456" />
28 35
 </appSettings>

+ 126 - 0
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/UploadFileController.cs

@@ -0,0 +1,126 @@
1
+using CallCenter.Utility;
2
+using CallCenterApi.Interface.Controllers.Base;
3
+using System;
4
+using System.Collections.Generic;
5
+using System.Diagnostics;
6
+using System.IO;
7
+using System.Linq;
8
+using System.Text;
9
+using System.Threading;
10
+using System.Web;
11
+using System.Web.Mvc;
12
+
13
+namespace CallCenterApi.Interface.Controllers
14
+{
15
+    public class UploadFileController : BaseController
16
+    {
17
+        // GET: UploadFile
18
+        //public ActionResult Index()
19
+        //{
20
+        //    return View();
21
+        //}
22
+
23
+        public ActionResult UploadToFTP()
24
+        {
25
+            ActionResult res = NoToken("未知错误,请重新登录");
26
+            #region 前后端分离上传并修改文件格式
27
+            //string fullFileName = this.txtfileUpload.FileName; //文件路径名
28
+            HttpPostedFile _upFile = RequestString.GetFile("upFile");
29
+            if (_upFile != null)
30
+            {
31
+                string datename = DateTime.Now.ToString("yyyyMMddHHMMss");
32
+                string fullFileName = datename + "_" + _upFile.FileName;
33
+                string path = Server.MapPath(this.Request.ApplicationPath + "~/upload/");
34
+                if (!System.IO.Directory.Exists(path))
35
+                {
36
+                    System.IO.Directory.CreateDirectory(path);
37
+                }
38
+                path = path + fullFileName;
39
+                string physicalpath = Server.MapPath(path);
40
+                _upFile.SaveAs(physicalpath);
41
+
42
+                #region 修改音频格式
43
+                if (!string.IsNullOrEmpty(physicalpath))
44
+                {
45
+                    //通过cmd执行ffmpeg改变音频采样率
46
+                    Process p = new Process();
47
+                    p.StartInfo.FileName = "cmd.exe";
48
+                    p.StartInfo.UseShellExecute = false;
49
+                    p.StartInfo.RedirectStandardOutput = true;
50
+                    p.StartInfo.RedirectStandardInput = true;
51
+                    p.StartInfo.RedirectStandardError = true;
52
+                    p.StartInfo.CreateNoWindow = true;
53
+                    //p.StartInfo.Arguments = "ffmpeg -i "+fileName+ " -ar 16000 "+ namePart + "New.wav";//参数以空格分隔,如果某个参数为空,可以传入””
54
+
55
+                    //p.Start();
56
+                    ////先进入cmd命令
57
+                    ////再打开FFmpeg.exe
58
+                    string ffmpegLocation = System.Threading.Thread.GetDomain().BaseDirectory;
59
+                    ////p.StandardInput.WriteLine("exit");
60
+                    //p.StandardInput.WriteLine("cd d/ " + ffmpegLocation + "ffmpeg.exe");
61
+                    //p.StandardInput.WriteLine(ffmpegLocation.TrimEnd('\\'));
62
+
63
+                    //判断是否存在文件夹,不存在则创建
64
+                    string voices_path = ffmpegLocation + "temp_voices";
65
+                    if (!Directory.Exists(voices_path))
66
+                    {
67
+                        Directory.CreateDirectory(voices_path);
68
+                    }
69
+                    string temp_filename = physicalpath.Split('/').Last().Split('.')[0] + "_temp_";
70
+
71
+                    p.Start();
72
+                    //先进入cmd命令
73
+                    //再打开FFmpeg.exe
74
+                    //p.StandardInput.WriteLine("exit");
75
+                    p.StandardInput.WriteLine("cd d/ " + ffmpegLocation + "ffmpeg.exe");
76
+                    p.StandardInput.WriteLine(ffmpegLocation.TrimEnd('\\'));
77
+
78
+                    //p.StandardInput.WriteLine("ffmpeg -i " + temp_filename + i + ".mp3 -f pcm -ar 16000 -ac 1 -ab 16 " + namepart.Replace(@"/", @"\"));
79
+                    //string changefile = "ffmpeg -i " + physicalpath + " -acodec pcm_s16le -f s16le -ar 16000 -ac 1 " + ffmpegLocation + temp_filename + ".pcm";
80
+                    string changefile = "ffmpeg -i " + physicalpath + " -acodec pcm_s16le -f s16le -ar 8000 -ac 1 " + ffmpegLocation + temp_filename + ".pcm";
81
+
82
+                    p.StandardInput.WriteLine(changefile);
83
+                    Thread.Sleep(3000);
84
+                    p.StandardInput.WriteLine("exit");
85
+                    //Kill 是终止没有图形化界面的进程的唯一方法。
86
+                    p.Kill();
87
+
88
+                    #region 上传
89
+                    uploadFile upfile = new uploadFile();
90
+                    //通过读取配置文件,获取数据库
91
+                    string _ftp = Configs.GetValue("ftp");
92
+                    string _acc = Configs.GetValue("account");
93
+                    string _pwd = Configs.GetValue("password");
94
+
95
+                    upfile.ftpPath = _ftp;
96
+                    upfile.ftpUserID = _acc;
97
+                    upfile.ftpPassword = _pwd;
98
+                    //FileInfo fi = new FileInfo(path);
99
+                    FileInfo fi = new FileInfo(physicalpath);
100
+                    //string res = upfile.UploadLocalToFtp(path);
101
+                    //string res = upfile.UploadLocalToFtp(physicalpath);
102
+                    string uploadres = upfile.UploadLocalToFtp(ffmpegLocation + temp_filename + ".pcm");
103
+                    if (uploadres == "上传成功!")
104
+                    {
105
+                        //写入日志
106
+                        string logfile = Server.MapPath("~/log.txt");
107
+                        if (!System.IO.File.Exists(logfile))
108
+                        {
109
+                            System.IO.File.Create(logfile);
110
+                        }
111
+                        //开始写入
112
+                        string str = DateTime.Now.ToString() + " 文件" + fullFileName + "上传成功";
113
+                        System.IO.File.AppendAllLines(logfile, new string[] { str }, Encoding.GetEncoding("gb2312"));
114
+                        res = Success("上传成功");
115
+                    }
116
+                    #endregion
117
+                }
118
+                #endregion
119
+            }
120
+            else
121
+                res = Error("参数传入失败");
122
+            #endregion
123
+            return res;
124
+        }
125
+    }
126
+}

+ 122 - 0
codegit/CallCenterCommon/CallCenter.Utility/uploadFile.cs

@@ -0,0 +1,122 @@
1
+using System;
2
+using System.Collections.Generic;
3
+using System.IO;
4
+using System.Linq;
5
+using System.Net;
6
+using System.Text;
7
+using System.Threading.Tasks;
8
+
9
+namespace CallCenter.Utility
10
+{
11
+    public class uploadFile
12
+    {
13
+        public string ftpPath;
14
+        public string ftpUserID;
15
+        public string ftpPassword;
16
+
17
+        /// <summary>
18
+        /// 上传文件
19
+        /// </summary>
20
+        /// <param name="fileinfo">需要上传的文件</param>
21
+        public string UploadLocalToFtp(string localFile)
22
+        {
23
+            string res = "";
24
+            if (!File.Exists(localFile))
25
+            {
26
+                res = "文件:“" + localFile + "” 不存在!";
27
+                return res;
28
+            }
29
+            FileInfo fileInf = new FileInfo(localFile);
30
+            string ftpURI = "ftp://" + ftpPath + "/";
31
+            string uri = ftpURI + fileInf.Name;
32
+            FtpWebRequest reqFTP;
33
+
34
+            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));// 根据uri创建FtpWebRequest对象   
35
+            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);// ftp用户名和密码  
36
+            reqFTP.KeepAlive = false;// 默认为true,连接不会被关闭 // 在一个命令之后被执行  
37
+            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;// 指定执行什么命令  
38
+            reqFTP.UseBinary = true;// 指定数据传输类型  
39
+            reqFTP.ContentLength = fileInf.Length;// 上传文件时通知服务器文件的大小  
40
+            int buffLength = 2048;// 缓冲大小设置为2kb  
41
+            byte[] buff = new byte[buffLength];
42
+            int contentLen;
43
+
44
+            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件  
45
+            FileStream fs = fileInf.OpenRead();
46
+            try
47
+            {
48
+                Stream strm = reqFTP.GetRequestStream();// 把上传的文件写入流  
49
+                contentLen = fs.Read(buff, 0, buffLength);// 每次读文件流的2kb  
50
+
51
+                while (contentLen != 0)// 流内容没有结束  
52
+                {
53
+                    // 把内容从file stream 写入 upload stream  
54
+                    strm.Write(buff, 0, contentLen);
55
+                    contentLen = fs.Read(buff, 0, buffLength);
56
+                }
57
+                // 关闭两个流  
58
+                strm.Close();
59
+                fs.Close();
60
+                res = "上传成功!";
61
+            }
62
+            catch (Exception ex)
63
+            {
64
+                res = "上传文件【" + ftpPath + "/" + fileInf.Name + "】时,发生错误:" + ex.Message + "<br/>";
65
+            }
66
+            return res;
67
+        }
68
+
69
+        /// <summary>  
70
+        /// 判断ftp服务器上该目录是否存在  
71
+        /// </summary>  
72
+        /// <param name="ftpPath">FTP路径目录</param>  
73
+        /// <param name="dirName">目录上的文件夹名称</param>  
74
+        /// <returns></returns>  
75
+        private bool CheckDirectoryExist(string ftpPath, string dirName)
76
+        {
77
+            bool flag = true;
78
+            try
79
+            {
80
+                //实例化FTP连接  
81
+                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath + dirName);
82
+                ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
83
+                ftp.Method = WebRequestMethods.Ftp.ListDirectory;
84
+                FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
85
+                response.Close();
86
+            }
87
+            catch (Exception)
88
+            {
89
+                flag = false;
90
+            }
91
+            return flag;
92
+        }
93
+
94
+        /// <summary>  
95
+        /// 创建文件夹    
96
+        /// </summary>    
97
+        /// <param name="ftpPath">FTP路径</param>    
98
+        /// <param name="dirName">创建文件夹名称</param>    
99
+        public void MakeDir(string ftpPath, string dirName)
100
+        {
101
+            FtpWebRequest reqFTP;
102
+            try
103
+            {
104
+                string ui = (ftpPath + dirName).Trim();
105
+                reqFTP = (FtpWebRequest)FtpWebRequest.Create(ui);
106
+                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
107
+                reqFTP.UseBinary = true;
108
+                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
109
+                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
110
+                Stream ftpStream = response.GetResponseStream();
111
+                ftpStream.Close();
112
+                response.Close();
113
+                //Response.Write("文件夹【" + dirName + "】创建成功!<br/>");
114
+            }
115
+            catch (Exception ex)
116
+            {
117
+                //Response.Write("新建文件夹【" + dirName + "】时,发生错误:" + ex.Message);
118
+            }
119
+
120
+        }
121
+    }
122
+}