whycrzh
2021-01-28 c8332186836b3dfe1fbd32d7bec7cd29a57e1888
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.fgkj.controller.media;
 
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User_inf;
import com.fgkj.util.ActionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.io.File;
import java.io.IOException;
import java.util.Date;
 
@RequestMapping("broadcast")
@RestController
@Api(tags = "broadcast接口")
public class BroadcastController{
    private static int createCount = 0;                //生成音频文件计数    
 
    @RequestMapping("create")
    @ApiOperation(notes = "",value="生成音频文件")
    public ServiceModel createBroadcast(@RequestBody MyMultiMedia media){
        MSTTSSpeech speech=new MSTTSSpeech();
        // MyMultiMedia media = ActionUtil.getGson().fromJson(json, MyMultiMedia.class);
        User_inf uinf = (User_inf)ActionUtil.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);
        }
        return model;
    }
    
    /**
     * 根据当前用户名获取对应的文件名称
     * @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;        
    }
 
}