項目用docker部署在了Linux服務器中,大附件分片上傳后需要進行文件合並,為提高合並速度,使用系統命令進行操作,代碼如下:
#region 執行Linux系統命令 try { //拼接合並命令中的文件字符串,sourcePath為文件塊所在目錄,targetPath為合並文件的目錄 var fileStr = ""; for (var i = 0; i < files.Length; i++) { fileStr += fileStr.Length > 0 ? " " + sourcePath + i.ToString() : sourcePath + i.ToString(); } //合並文件命令(cat /home/test/1.txt home/test/2.txt >home/test/all.txt) string command = "cat " + fileStr + " >" + targetPath.Replace("\\", "/"); //執行結果 string result = ""; using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) { proc.StartInfo.FileName = "/bin/bash"; proc.StartInfo.Arguments = "-c \" " + command + " \""; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.Start(); result += proc.StandardOutput.ReadToEnd(); result += proc.StandardError.ReadToEnd(); proc.WaitForExit(); } } catch (Exception ex) { logger.Error("合並報錯:" + ex.Message); } #endregion