UploadVideoServlet.java
10.8 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.bsth.data.zndd.voice;
import com.alibaba.fastjson.JSONObject;
import com.bsth.data.BasicData;
import com.bsth.data.zndd.baidu.speech.restapi.asrdemo.AsrMain;
import com.bsth.entity.StationRoute;
import com.bsth.entity.sys.SysUser;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.StationRouteService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class UploadVideoServlet extends HttpServlet {
@Autowired
StationRouteService stationRouteService;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("1");
}
//如果出现多线程的情况下 识别文件名称可以加上专属账号的属性
public void doPost(String line,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
String savePath = "/pcm";
File file = new File(savePath);
// 判断上传文件的保存目录是否存在
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath + "目录不存在,需要创建");
// 创建目录
file.mkdir();
}
// 消息提示
String message = "";
try {
String filename = null;
// 使用Apache文件上传组件处理文件上传步骤:
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
// 3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照传统方式获取数据
return;
}
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
Iterator<String> iterator = req.getFileNames();
String[] value = new String[5];
int i = 0;
while (iterator.hasNext()) {
MultipartFile item = req.getFile(iterator.next());
SysUser user = SecurityUtils.getCurrentUser();
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = "test_"+user.getUserName()+".wav";
//item.getName();
//System.out.println(filename);
// 获取item中的上传文件的输入流
InputStream in = item.getInputStream();
// 创建一个文件输出流
FileOutputStream out = new FileOutputStream(savePath + "/" + filename);
// 创建一个缓冲区
byte buffer[] = new byte[1024];
// 判断输入流中的数据是否已经读完的标识
int len = 0;
// 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = in.read(buffer)) > 0) {
// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"
// + filename)当中
out.write(buffer, 0, len);
}
// 关闭输入流
in.close();
// 关闭输出流
out.close();
// 删除处理文件上传时生成的临时文件
//item.delete();
message = "文件上传成功!";
//System.out.println(message);
/*VoiceUtil voiceUtil= new VoiceUtil();
String str = voiceUtil.getWord(savePath + "/" + filename);
str = str.replace(" ","");*/
//语音转文字
AsrMain demo = new AsrMain();
// 填写下面信息
String result = demo.run();
JSONObject json = JSONObject.parseObject(result);
List textList = ( List)json.get("result");
String str = textList.get(0).toString().replace("。","");
System.out.println("语音解析:"+str);
//定义输出类型
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
List<retun> list1 = new ArrayList<>();
String text = textList.get(0).toString();
//帮我添加一个夏硕路林兰路到鸿音路云端路的班次
String fcsj = null;//线路名,上下行,发车时间
String[] ks = text.replace(",","").split("到");
String Station1= ks[0].split("个")[1];
String Station2= ks[1].split("的")[0];
//发车时间
fcsj = extractTimes(text);
//线路名称 - 线路编码和名称对照
Map<String, Object> param = new HashMap<>();
param.put("lineCode_eq", line);
param.put("directions_eq", 0);
param.put("destroy_eq", 0);
List<StationRoute> stationup = ((List<StationRoute>) stationRouteService.list(param));
//上行匹配
Map m = pyPp(stationup,Station1,Station2);
if (m.get("stationcode1") == null || m.get("stationcode2") == null){
param.put("directions_eq",1);
List<StationRoute> stationdown = ((List<StationRoute>) stationRouteService.list(param));
//下行匹配
Map smap = pyPp(stationdown,Station1,Station2);
if (smap.get("stationcode1") == null || smap.get("stationcode2") == null){
writer.write(text +","+"99999");
return;
}
writer.write(text +","+smap.get("stationcode1")+","+smap.get("stationcode2")+","+1);
return;
}
writer.write(text +","+m.get("stationcode1")+","+m.get("stationcode2")+","+0);
return;
//打开0 关闭1 临加2 999异常
}
} catch (Exception e) {
message = "文件上传失败!";
System.out.println(message);
e.printStackTrace();
}
}
public static Map pyPp(List<StationRoute> stationss,String Station1,String Station2){
Double sity1 = 0D,sity2 = 0D; //拼音匹配百分比
Map m = new HashMap();
String stationcode1 = null,stationcode2 = null;//匹配的站点
Boolean st2 = false;
for (StationRoute st : stationss){
sity1 = calculateSimilarity(st.getStationName(), Station1);
if (sity1 >= 0.75 && !st2){
stationcode1 = st.getStationCode();
st2 =true;
}
if (st2){
sity2 = calculateSimilarity(st.getStationName(), Station2);
if(sity2 >= 0.75){
stationcode2 = st.getStationCode();
}
}
}
m.put("stationcode1",stationcode1);
m.put("stationcode2",stationcode2);
return m;
}
private static final Pattern TIME_PATTERN =
Pattern.compile("([0-5][0-9]:[0-5][0-9])");
//获取添加班次时间
public static String extractTimes(String text) {
Matcher matcher = TIME_PATTERN.matcher(text);
while (matcher.find()) {
return matcher.group();
}
return "";
}
class retun {
private String tite;
private String txt;
public String getTite() {
return tite;
}
public void setTite(String tite) {
this.tite = tite;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
public static String getPinyin(String chinese) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuilder pinyinBuilder = new StringBuilder();
char[] charArray = chinese.toCharArray();
for (char c : charArray) {
String[] pinyinArray = null;
try {
pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
if (pinyinArray != null) {
pinyinBuilder.append(pinyinArray[0]);
} else {
pinyinBuilder.append(c);
}
}
return pinyinBuilder.toString();
}
public static Double calculateSimilarity(String str1, String str2) {
String pinyinStr1 = getPinyin(str1);
String pinyinStr2 = getPinyin(str2);
int len1 = pinyinStr1.length();
int len2 = pinyinStr2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = 0;
}
for (int j = 0; j <= len2; j++) {
dp[0][j] = 0;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (pinyinStr1.charAt(i - 1) == pinyinStr2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return Double.valueOf(dp[len1][len2]) / len1;
}
}