package com.fgkj.actions.media; import java.io.File; import java.io.IOException; import java.util.Date; import com.fgkj.actions.ActionUtil; import com.fgkj.dto.ServiceModel; import com.fgkj.dto.User_inf; public class BroadcastAction extends ActionUtil{ private static int createCount = 0; //生成音频文件计数 private String json; private String result; public String createBroadcast(){ MSTTSSpeech speech=new MSTTSSpeech(); MyMultiMedia media = ActionUtil.getGson().fromJson(json, MyMultiMedia.class); User_inf uinf = (User_inf)getUser(); media.setMedia_name(getMediaName(uinf.getUName(),MSTTSSpeech.MYMEDIA_WAV)); media.setMedia_path(getRootFilePath(media.getMedia_name())); boolean flag =speech.saveToWav(media.getMedia_text(), media.getMedia_path()); createCount++; if(createCount % 50 == 0){ //清空之前生成的语音文件 if(createCount > 100000000){ createCount = 1; } } ServiceModel model = new ServiceModel(); if(flag){ model.setCode(1); model.setData(media); }else{ model.setCode(0); model.setData(media); } result = tojson(model); return SUCCESS; } /** * 根据当前用户名获取对应的文件名称 * @param uname * @param fileType * @return */ public String getMediaName(String uname,String fileType){ return (uname!=null?uname:"")+"_"+(new Date().getTime())+fileType; } public String getRootFilePath(String fileName){ String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/"); String str = new File(loadpath).getParentFile().getAbsolutePath(); String root = str+"/mediafiles/"; // 上传路径 String filePath = root+"/"+fileName; File f = new File(filePath); if(!f.exists()){ if(!f.getParentFile().exists()){ f.getParentFile().mkdirs(); } try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } return filePath; } public String getResult() { return result; } public void setJson(String json) { this.json = json; } }