修改音视频文件样例配置功能,表单界面删除样例文件的上传,改由后台调用外部程序生成样例文件。
Signed-off-by: zhangwei <zhangwei@intranet.com>
This commit is contained in:
@@ -2,15 +2,20 @@ package com.nis.web.service.configuration;
|
||||
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@@ -23,6 +28,7 @@ import com.nis.domain.maat.ToMaatResult;
|
||||
import com.nis.domain.maat.ToMaatResult.ResponseData;
|
||||
import com.nis.exceptions.MaatConvertException;
|
||||
import com.nis.util.ConfigServiceUtil;
|
||||
import com.nis.util.Constants;
|
||||
import com.nis.util.FileUtils;
|
||||
import com.nis.util.StringUtil;
|
||||
import com.nis.web.dao.configuration.AvCfgDao;
|
||||
@@ -86,7 +92,77 @@ public class AvCfgService extends BaseService{
|
||||
}
|
||||
if(compileId!=0){
|
||||
entity.setCompileId(compileId);
|
||||
entity.setIsSampleCreated(0);
|
||||
File uploadSrcFile = new File(entity.getSrcPath());
|
||||
String srcMd5 = FileUtils.getFileMD5(uploadSrcFile);
|
||||
entity.setSrcMd5(srcMd5);
|
||||
avCfgDao.insertAvFileSample(entity);
|
||||
|
||||
//调用外部脚本,生成样例文件
|
||||
List list = new ArrayList();
|
||||
Map<String,Object> map = new HashMap();
|
||||
map.put("srcFile", entity.getSrcPath());
|
||||
map.put("dstFile", entity.getSamplePath());
|
||||
map.put("resultFile", entity.getResultPath());
|
||||
map.put("fileId", entity.getCfgId());
|
||||
list.add(map);
|
||||
String param = gsonToJson(list);
|
||||
String sampleCreatelProc = "";
|
||||
if(entity.getCfgType().equals(Constants.AV_SAMPLE_AUDIO_REGION)||
|
||||
entity.getCfgType().equals(Constants.AV_SAMPLE_VOIP_REGION)){//音频样例配置或VOIP样例配置
|
||||
sampleCreatelProc = Constants.AUDIO_SAMPLE_CREATE_PROC;
|
||||
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
|
||||
param = StringEscapeUtils.escapeJava(param);
|
||||
}
|
||||
if(Constants.AUDIO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
|
||||
param = "'"+param+"'";
|
||||
}
|
||||
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_VEDIO_REGION)){//视频样例配置
|
||||
sampleCreatelProc = Constants.VEDIO_SAMPLE_CREATE_PROC;
|
||||
if(Constants.VEDIO_SAMPLE_PROC_PARAM_IS_TRANSLATION){
|
||||
param = StringEscapeUtils.escapeJava(param);
|
||||
}
|
||||
if(Constants.VEDIO_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
|
||||
param = "'"+param+"'";
|
||||
}
|
||||
}else if(entity.getCfgType().equals(Constants.AV_SAMPLE_PICTURE_REGION)){//图片样例配置
|
||||
sampleCreatelProc = Constants.PICTURE_SAMPLE_CREATE_PROC;
|
||||
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_TRANSLATION){
|
||||
param = StringEscapeUtils.escapeJava(param);
|
||||
}
|
||||
if(Constants.PICTURE_SAMPLE_PROC_PARAM_IS_QUOTATION){//json参数是否需要前后单引号处理
|
||||
param = "'"+param+"'";
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("调用外部程序输入参数:"+param);
|
||||
Map resultMap = execShell(sampleCreatelProc,param);
|
||||
logger.info("调用外部程序结果:"+resultMap);
|
||||
|
||||
if(resultMap.get("exitStatus").equals(0)){//调用外部程序成功
|
||||
String out = resultMap.get("out").toString();//输出参数
|
||||
JSONArray resArray =JSONArray.fromObject(out);
|
||||
JSONObject resObject = resArray.getJSONObject(0);
|
||||
logger.info("调用外部程序输出参数:"+resObject);
|
||||
int state = resObject.getInt("state");
|
||||
int fileSize = resObject.getInt("fileSize");
|
||||
Integer fileId = resObject.getInt("fileId");
|
||||
if(state==1 && fileSize>0){//成功
|
||||
entity.setIsSampleCreated(1);//样例文件创建成功
|
||||
}else{
|
||||
entity.setIsSampleCreated(-1);//样例文件创建失败
|
||||
}
|
||||
if(fileId.equals(entity.getCfgId().intValue())){
|
||||
File uploadSampleFile = new File(entity.getSamplePath());
|
||||
String sampleMd5 = FileUtils.getFileMD5(uploadSampleFile);
|
||||
entity.setSampleMd5(sampleMd5);
|
||||
avCfgDao.updateAvFileSample(entity);
|
||||
}else{
|
||||
logger.info("调用外部程序返回fileId错误,输入的fileId为:"+entity.getCfgId()+",输出的fileId为:"+fileId);
|
||||
}
|
||||
}else{
|
||||
throw new MaatConvertException(resultMap.get("error")!=null?resultMap.get("error").toString():resultMap.get("message").toString());
|
||||
}
|
||||
}else{
|
||||
throw new MaatConvertException("<spring:message code=\"request_service_failed\"/>");
|
||||
}
|
||||
@@ -212,4 +288,73 @@ public class AvCfgService extends BaseService{
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 调用shell脚本 返回运行结果
|
||||
*
|
||||
* @param shellName
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public Map<String, Object> execShell(String shellName,
|
||||
String... params) {
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(shellName);
|
||||
for (String temp : params) {
|
||||
sb.append(" " + temp);
|
||||
}
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
String cmd1 = "";
|
||||
String cmd2 = "";
|
||||
if(os.contains("windows")){
|
||||
cmd1 = "cmd.exe";
|
||||
cmd2 = "/c";
|
||||
}else{
|
||||
cmd1 = "/bin/sh";
|
||||
cmd2 = "-c";
|
||||
}
|
||||
logger.info("调用脚本信息,cmd1:"+cmd1+",cmd2:"+cmd2);
|
||||
String cmdarray[] = new String[] {cmd1, cmd2 ,sb.toString() };
|
||||
BufferedReader br = null;
|
||||
BufferedReader bre = null;
|
||||
try {
|
||||
Process exec = Runtime.getRuntime().exec(cmdarray);
|
||||
exec.getInputStream();
|
||||
br = new BufferedReader(
|
||||
new InputStreamReader(exec.getInputStream()));
|
||||
bre = new BufferedReader(new InputStreamReader(
|
||||
exec.getErrorStream()));
|
||||
String s = null;
|
||||
StringBuilder out = new StringBuilder();
|
||||
while ((s = br.readLine()) != null) {
|
||||
out.append(s);
|
||||
}
|
||||
result.put("out", out.toString());//输出参数
|
||||
out.setLength(0);//清空
|
||||
while ((s = bre.readLine()) != null) {
|
||||
out.append(s);
|
||||
}
|
||||
result.put("error", out.toString());//错误信息
|
||||
int waitFor = exec.waitFor();
|
||||
logger.info("调用脚本:"+shellName+",执行返回状态值:"+waitFor);
|
||||
result.put("exitStatus", waitFor);//执行状态
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.error("调用 " + shellName + " 脚本异常", e);
|
||||
} finally {
|
||||
if (br != null)
|
||||
try {
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (bre != null)
|
||||
try {
|
||||
bre.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user