public void listenWarn(){
BufferedReader reader = null;
try {
URL url = new URL("http://192.168.170.175:18080/api/emit?browserId="+ UUID.fastUUID().toString());
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// 这儿根据自己的情况选择get或post
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Charset", "UTF-8");
//读取过期时间(很重要,建议加上)
urlConnection.setReadTimeout(60 * 1000);
// text/plain模式
urlConnection.setRequestProperty("Content-Type", "text/plain; charset=UTF-8");
InputStream inputStream = urlConnection.getInputStream();
InputStream is = new BufferedInputStream(inputStream);
reader = new BufferedReader(new InputStreamReader(is));
String line = "";
// TODO: 2022/3/29 data:{"alarmDescription":"陌生人","alarmTime":"2022-03-29 10:18:07","pic":"https://192.168.111.147:8082/local/home/tc-eb5/local/localstore/16485/facerecognize_9765d7d3-ae4f-461b-8532-02f016b00bdc.jpg","deviceId":"IPC-ff8081817fbaac5b017fbb0390870007"}
//生成一条告警
while ((line = reader.readLine()) != null)
//while (StringUtils.isNotEmpty(line))
{
// 处理数据接口
String replace = line.replace("data:", "");
JSONObject jsonObject1 = JSONObject.parseObject(replace);
String alarmDescription = String.valueOf(jsonObject1.get("alarmDescription"));
String alarmTimeS = String.valueOf(jsonObject1.get("alarmTime"));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date alarmTime = simpleDateFormat.parse(alarmTimeS);
String alarmPhoto = String.valueOf(jsonObject1.get("pic"));
String alarmNo = String.valueOf(jsonObject1.get("deviceId"));
AlarmVO alarmVO = new AlarmVO();
alarmVO.setAlarmName(alarmDescription);
alarmVO.setAlarmTime(alarmTime);
alarmVO.setAlarmPhoto(alarmPhoto);
alarmVO.setAlarmNo(alarmNo);
alarmVO.setMachineRoom("黄山数据中心");
alarmVO.setRoomId(6);
alarmMapper.insertAlarm(alarmVO);
}
}catch (Exception e){
try {
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
listenWarn();
}
}
从未连接的数据库获取数据
最新推荐文章于 2024-04-26 17:40:25 发布