搭建智慧城乡项目,并实现mq消息订阅,websocket推送报警信息等功能
This commit is contained in:
230
src/main/java/com/xkrs/util/DateTimeUtil.java
Normal file
230
src/main/java/com/xkrs/util/DateTimeUtil.java
Normal file
@ -0,0 +1,230 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 日期时间工具
|
||||
* @author tajochen
|
||||
*/
|
||||
public class DateTimeUtil {
|
||||
|
||||
private final static String COMMON_PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss";
|
||||
private final static String COMMON_PATTERN_DATE = "yyyy-MM-dd";
|
||||
private final static DateTimeFormatter COMMON_FORMATTER_DATETIME = DateTimeFormatter.ofPattern(COMMON_PATTERN_DATETIME);
|
||||
private final static DateTimeFormatter COMMON_FORMATTER_DATE = DateTimeFormatter.ofPattern(COMMON_PATTERN_DATE);
|
||||
private final static ZoneOffset DEFAULT_ZONE_OFFSET = ZoneOffset.of("+8");
|
||||
|
||||
/**
|
||||
* 字符串转LocalDate
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate stringToDate(String date) {
|
||||
assert date != null;
|
||||
return LocalDate.parse(date, COMMON_FORMATTER_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDate转字符串
|
||||
* @param date
|
||||
* @return
|
||||
*/
|
||||
public static String dateToString(LocalDate date) {
|
||||
assert date != null;
|
||||
return COMMON_FORMATTER_DATE.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime转字符串
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
public static String dateTimeToString(LocalDateTime dateTime) {
|
||||
assert dateTime != null;
|
||||
return COMMON_FORMATTER_DATETIME.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转LocalDateTime
|
||||
* @param dateStr
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime stringToDateTime(String dateStr) {
|
||||
assert dateStr != null;
|
||||
return LocalDateTime.parse(dateStr, COMMON_FORMATTER_DATETIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转Instant时间戳
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static Instant stringToInstant(String str) {
|
||||
assert str != null;
|
||||
return stringToDateTime(str).toInstant(DEFAULT_ZONE_OFFSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* LocalDateTime转字符串(格式化)
|
||||
* @param dateTime
|
||||
* @param formatter
|
||||
* @return
|
||||
*/
|
||||
public static String dateToStringFormatter(LocalDateTime dateTime, DateTimeFormatter formatter) {
|
||||
assert dateTime != null;
|
||||
return formatter.format(dateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转LocalDateTime(格式化)
|
||||
* @param dateStr
|
||||
* @param formatter
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime stringToDateTimeFormatter(String dateStr, DateTimeFormatter formatter) {
|
||||
assert dateStr != null;
|
||||
return LocalDateTime.parse(dateStr, formatter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转 local date
|
||||
* @param dateStr
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate stringToDateFormatter(String dateStr){
|
||||
LocalDate date = LocalDate.parse(dateStr, COMMON_FORMATTER_DATE);
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 日期转时间戳
|
||||
* @param dateTime
|
||||
* @return
|
||||
*/
|
||||
public static long dateToTimeMillis(LocalDateTime dateTime) {
|
||||
assert dateTime != null;
|
||||
return dateTime.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli();
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转日期
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime timeMillisToDate(long timeMillis) {
|
||||
Instant instant = Instant.ofEpochMilli(timeMillis);
|
||||
return LocalDateTime.ofInstant(instant, DEFAULT_ZONE_OFFSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转时间
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime timeMillisToTime(long timeMillis) {
|
||||
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
|
||||
return localDateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转时间再转字符串
|
||||
* @param timeMillis
|
||||
* @return
|
||||
*/
|
||||
public static String timeMillisToString(long timeMillis){
|
||||
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(timeMillis, 0, ZoneOffset.ofHours(8));
|
||||
return COMMON_FORMATTER_DATETIME.format(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间 hh:mm:ss:nnn
|
||||
* @return
|
||||
*/
|
||||
public static LocalDateTime getNowTime() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return now;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期 yyyy-MM-dd
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate getToday() {
|
||||
LocalDate now = LocalDate.now();
|
||||
return now;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 Instant 时间戳
|
||||
* @return
|
||||
*/
|
||||
public static Instant getInstant() {
|
||||
Instant timeStamp = Instant.now();
|
||||
return timeStamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间 yyyy-MM-ss hh:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static String getNowTimeStr(){
|
||||
return COMMON_FORMATTER_DATETIME.format(getNowTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断日期格式是否合法 "yyyy-MM-dd"
|
||||
* @param strDate
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValidDate(String strDate) {
|
||||
final int minLen = 10;
|
||||
if(strDate == null || strDate.length() < minLen) {
|
||||
return false;
|
||||
}
|
||||
//正则表达式校验日期格式 yyyy-MM-dd
|
||||
String eL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-" +
|
||||
"(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})" +
|
||||
"(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)(([0-9]{3}[1-9]|[0-9]{2}[1-9]" +
|
||||
"[0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-" +
|
||||
"(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" +
|
||||
"((0[48]|[2468][048]|[3579][26])00))-02-29)";
|
||||
Pattern pat = Pattern.compile(eL);
|
||||
Matcher matcher = pat.matcher(strDate);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断时间格式 格式必须为 "YYYY-MM-DD HH:mm:ss"
|
||||
* @param sDateTime
|
||||
* @return
|
||||
*/
|
||||
public static boolean isValidDateTime(String sDateTime) {
|
||||
final int minLen = 19;
|
||||
if ((sDateTime == null) || (sDateTime.length() < minLen)) {
|
||||
return false;
|
||||
}
|
||||
String eL = "(((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-(0?[13578]|1[02])-" +
|
||||
"(0?[1-9]|[12]\\\\d|3[01]))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-" +
|
||||
"(0?[13456789]|1[012])-(0?[1-9]|[12]\\\\d|30))|((01[0-9]{2}|0[2-9][0-9]{2}|[1-9][0-9]{3})-0?2-" +
|
||||
"(0?[1-9]|1\\\\d|2[0-8]))|(((1[6-9]|[2-9]\\\\d)(0[48]|[2468][048]|[13579][26])|((04|08|12|16|[2468][048]|" +
|
||||
"[3579][26])00))-0?2-29)) (20|21|22|23|[0-1]?\\\\d):[0-5]?\\\\d:[0-5]?\\\\d";
|
||||
Pattern pat = Pattern.compile(eL);
|
||||
Matcher matcher = pat.matcher(sDateTime);
|
||||
return matcher.matches();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String mm = "1626340246";
|
||||
long l = Long.parseLong(mm);
|
||||
String timeMillisToString = timeMillisToString(l);
|
||||
System.out.println(timeMillisToString);
|
||||
}
|
||||
|
||||
}
|
33
src/main/java/com/xkrs/util/Md5Util.java
Normal file
33
src/main/java/com/xkrs/util/Md5Util.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
public class Md5Util {
|
||||
|
||||
public static String stringToMd5(String content){
|
||||
byte[] secretBytes = null;
|
||||
try {
|
||||
secretBytes = MessageDigest.getInstance("md5").digest(
|
||||
content.getBytes());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("没有md5这个算法!");
|
||||
}
|
||||
// 16进制数字
|
||||
String md5code = new BigInteger(1, secretBytes).toString(16);
|
||||
// 如果生成数字未满32位,需要前面补0
|
||||
for (int i = 0; i < 32 - md5code.length(); i++) {
|
||||
md5code = "0" + md5code;
|
||||
}
|
||||
return md5code;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s = stringToMd5("12345");
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
372
src/main/java/com/xkrs/util/RequestUtil.java
Normal file
372
src/main/java/com/xkrs/util/RequestUtil.java
Normal file
@ -0,0 +1,372 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||
import org.apache.http.entity.mime.content.StringBody;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 上传工具
|
||||
* @author XinYi Song
|
||||
**/
|
||||
public class RequestUtil {
|
||||
|
||||
private static final String FIREFOX_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0";
|
||||
private static final String CHROME_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
|
||||
"(KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36";
|
||||
/**
|
||||
* 模拟 get请求
|
||||
* @param url 链接
|
||||
* @param map 参数列表
|
||||
*/
|
||||
public static String getStandard(String url, Map<String,String> map) {
|
||||
String body = "";
|
||||
// 1.拿到一个httpclient的对象
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
// 2.1 提交请求体
|
||||
ArrayList<NameValuePair> parameters = new ArrayList<>();
|
||||
URIBuilder builder = null;
|
||||
// 装填参数
|
||||
if(map!=null){
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
builder = new URIBuilder(url);
|
||||
builder.setParameters(parameters);
|
||||
// 2.设置请求方式和请求信息
|
||||
HttpGet httpGet = new HttpGet(builder.build());
|
||||
// 2.1 提交header头信息
|
||||
httpGet.addHeader("user-agent", FIREFOX_UA);
|
||||
// 3.执行请求
|
||||
response = httpClient.execute(httpGet);
|
||||
// 4.获取返回值
|
||||
assert response != null;
|
||||
// 按指定编码转换结果实体为String类型
|
||||
body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||
// 5.关闭连接
|
||||
response.close();
|
||||
httpClient.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟get请求(加请求头)
|
||||
* @param url
|
||||
* @param param
|
||||
* @param token
|
||||
* @return
|
||||
*/
|
||||
public static String doGet(String url, Map<String, String> param,String token) {
|
||||
|
||||
final int okResNum = 200;
|
||||
|
||||
String result = null;
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
URIBuilder builder = new URIBuilder(url);
|
||||
if (param != null) {
|
||||
for (String key : param.keySet()) {
|
||||
builder.addParameter(key, param.get(key));
|
||||
}
|
||||
}
|
||||
URI uri = builder.build();
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
httpGet.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
|
||||
httpGet.addHeader("Authorization",token);
|
||||
response = httpclient.execute(httpGet);
|
||||
if (response.getStatusLine().getStatusCode() == okResNum) {
|
||||
result = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 带参数的get请求
|
||||
* @param url
|
||||
* @param param
|
||||
* @return String
|
||||
*/
|
||||
public static JsonNode doGetJsonNode(String url, Map<String, String> param) {
|
||||
// 创建Httpclient对象
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
|
||||
String resultString = "";
|
||||
JsonNode node = null;
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
// 创建uri
|
||||
URIBuilder builder = new URIBuilder(url);
|
||||
if (param != null) {
|
||||
for (String key : param.keySet()) {
|
||||
builder.addParameter(key, param.get(key));
|
||||
}
|
||||
}
|
||||
URI uri = builder.build();
|
||||
// 创建http GET请求
|
||||
HttpGet httpGet = new HttpGet(uri);
|
||||
// 执行请求
|
||||
response = httpclient.execute(httpGet);
|
||||
// 判断返回状态是否为200
|
||||
if (response.getStatusLine().getStatusCode() == 200) {
|
||||
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
//使用ObjectMapper对象对 User对象进行转换
|
||||
node = objectMapper.readTree(resultString);
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟Post请求 application/x-www-form-urlencoded
|
||||
* @param url 资源地址
|
||||
* @param map 参数列表
|
||||
* @return
|
||||
*/
|
||||
public static String postStandard(String url, Map<String,String> map) {
|
||||
String body = "";
|
||||
// 创建httpclient对象
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
// 创建post方式请求对象
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
// 装填参数
|
||||
List<NameValuePair> nvps = new ArrayList<>();
|
||||
if(map!=null){
|
||||
for (Map.Entry<String, String> entry : map.entrySet()) {
|
||||
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
try {
|
||||
// 设置参数到请求对象中
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
|
||||
// 设置header报文头信息
|
||||
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||||
httpPost.setHeader("Accept", "application");
|
||||
httpPost.setHeader("Accept-Encoding", "gzip, deflate");
|
||||
// 执行请求操作,并拿到结果(同步阻塞)
|
||||
CloseableHttpResponse response = client.execute(httpPost);
|
||||
// 获取结果实体
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
// 按指定编码转换结果实体为String类型
|
||||
body = EntityUtils.toString(entity, "UTF-8");
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
// 释放链接
|
||||
response.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* 传送json类型的post请求
|
||||
* @param url
|
||||
* @param json
|
||||
* @return String
|
||||
*/
|
||||
public static String doPostJson(String url, String json) {
|
||||
// 创建Httpclient对象
|
||||
// CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpClient httpClient = SeeSSLCloseableHttpClient.getCloseableHttpClient();
|
||||
CloseableHttpResponse response = null;
|
||||
String resultString = "";
|
||||
try {
|
||||
// 创建Http Post请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
// 创建请求内容
|
||||
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
|
||||
httpPost.setEntity(entity);
|
||||
httpPost.setHeader("Content-Type","application/json");
|
||||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||||
// 执行http请求
|
||||
response = httpClient.execute(httpPost);
|
||||
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
|
||||
|
||||
public static String doPostJsonGetData(String url, String json, String token) {
|
||||
// 创建Httpclient对象
|
||||
// CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
CloseableHttpClient httpClient = SeeSSLCloseableHttpClient.getCloseableHttpClient();
|
||||
CloseableHttpResponse response = null;
|
||||
String resultString = "";
|
||||
try {
|
||||
// 创建Http Post请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
// 创建请求内容
|
||||
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
|
||||
httpPost.setEntity(entity);
|
||||
httpPost.setHeader("Content-Type","application/json");
|
||||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||||
httpPost.setHeader("X-Subject-Token", token);
|
||||
// 执行http请求
|
||||
response = httpClient.execute(httpPost);
|
||||
resultString = EntityUtils.toString(response.getEntity(), "utf-8");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return resultString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟Post请求 multipart/form-data
|
||||
* @param postFile 请求文件
|
||||
* @param postUrl 请求链接
|
||||
* @param postParam 请求参数
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,Object> postFile(File postFile, String postUrl, Map<String,String> postParam) {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(RequestUtil.class);
|
||||
Map<String,Object> resultMap = new HashMap<>(16);
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
try{
|
||||
// 把一个普通参数和文件上传给下面这个地址
|
||||
HttpPost httpPost = new HttpPost(postUrl);
|
||||
|
||||
// 设置传输参数
|
||||
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
|
||||
// 解决乱码问题
|
||||
multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||
multipartEntity.setCharset(StandardCharsets.UTF_8);
|
||||
|
||||
// 设置文件参数 ,获取文件名postFile.getName()
|
||||
// 把文件转换成流对象FileBody
|
||||
multipartEntity.addBinaryBody("file",postFile,ContentType.create("multipart/form-data",
|
||||
Consts.UTF_8),postFile.getName());
|
||||
// 设计文件以外的参数
|
||||
Set<String> keySet = postParam.keySet();
|
||||
for (String key : keySet) {
|
||||
// 相当于<input type="text" name="name" value=name>
|
||||
multipartEntity.addPart(key, new StringBody(postParam.get(key), ContentType.create(
|
||||
"multipart/form-data", Consts.UTF_8)));
|
||||
}
|
||||
HttpEntity reqEntity = multipartEntity.build();
|
||||
httpPost.setEntity(reqEntity);
|
||||
// 设置header报文头信息
|
||||
httpPost.setHeader("User-Agent", FIREFOX_UA);
|
||||
httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
httpPost.setHeader("Accept-Encoding","gzip, deflate");
|
||||
|
||||
// 发起请求,返回请求的响应
|
||||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
|
||||
resultMap.put("statusCode", response.getStatusLine().getStatusCode());
|
||||
// 获取响应对象
|
||||
HttpEntity resEntity = response.getEntity();
|
||||
if (resEntity != null) {
|
||||
// 打印响应内容
|
||||
resultMap.put("data", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
|
||||
resultMap.put("message", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
|
||||
resultMap.put("status", EntityUtils.toString(resEntity, StandardCharsets.UTF_8));
|
||||
}
|
||||
// 销毁
|
||||
EntityUtils.consume(resEntity);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
httpClient.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
log.info("上传结果:" + resultMap);
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String url1 = "https://111.26.161.203:443/admin/API/accounts/authorize";
|
||||
String json1 = "{\n" +
|
||||
" \"userName\": system,\n" +
|
||||
" \"ipAddress\": \"\",\n" +
|
||||
" \"clientType\": \"WINPC\"\n" +
|
||||
"}";
|
||||
String doPostJson = doPostJson(url1, json1);
|
||||
/*JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("userName","system");
|
||||
jsonObject.put("ipAddress","");
|
||||
jsonObject.put("clientType","WINPC");*/
|
||||
|
||||
System.out.println(doPostJson);
|
||||
}
|
||||
}
|
64
src/main/java/com/xkrs/util/SeeSSLCloseableHttpClient.java
Normal file
64
src/main/java/com/xkrs/util/SeeSSLCloseableHttpClient.java
Normal file
@ -0,0 +1,64 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
import org.apache.http.config.Registry;
|
||||
import org.apache.http.config.RegistryBuilder;
|
||||
import org.apache.http.conn.HttpClientConnectionManager;
|
||||
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
|
||||
public class SeeSSLCloseableHttpClient {
|
||||
|
||||
private static X509TrustManager tm = new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
public static CloseableHttpClient getCloseableHttpClient() {
|
||||
SSLContext ctx = null;
|
||||
try {
|
||||
ctx = SSLContext.getInstance("TLS");
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
HttpClientBuilder builder = HttpClientBuilder.create();
|
||||
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
builder.setSSLSocketFactory(sslConnectionFactory);
|
||||
|
||||
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||
.register("https", sslConnectionFactory).build();
|
||||
|
||||
HttpClientConnectionManager ccm1 = new BasicHttpClientConnectionManager(registry);
|
||||
|
||||
builder.setConnectionManager(ccm1);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
113
src/main/java/com/xkrs/util/TopicConsumerListener.java
Normal file
113
src/main/java/com/xkrs/util/TopicConsumerListener.java
Normal file
@ -0,0 +1,113 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
/**
|
||||
* @Author: XinYi Song
|
||||
* @Date: 2022/2/8 14:42
|
||||
*/
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xkrs.dao.FireDao;
|
||||
import com.xkrs.model.entity.Fire;
|
||||
import com.xkrs.websocket.server.WebSocketServer;
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.transaction.Transactional;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class TopicConsumerListener {
|
||||
|
||||
@Resource
|
||||
private FireDao fireDao;
|
||||
|
||||
/**
|
||||
* topic模式的消费者
|
||||
* 订阅火情报警信息
|
||||
* @param message
|
||||
*/
|
||||
@JmsListener(destination="${spring.activemq.topic-name}", containerFactory="topicListener")
|
||||
public void readActiveQueue(String message) throws IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(message);
|
||||
String method = jsonNode.get("method").asText();
|
||||
if("vms.alarm.msg".equals(method)){
|
||||
JsonNode info = jsonNode.get("info");
|
||||
Fire fire = new Fire();
|
||||
fire.setDeviceCode(info.get("deviceCode").asText());
|
||||
fire.setChannelSeq(info.get("channelSeq").asText());
|
||||
fire.setUnitType(info.get("unitType").asText());
|
||||
fire.setNodeType(info.get("nodeType").asText());
|
||||
fire.setNodeCode(info.get("nodeCode").asText());
|
||||
String alarmCode = info.get("alarmCode").asText();
|
||||
fire.setAlarmCode(alarmCode);
|
||||
fire.setAlarmStat(info.get("alarmStat").asText());
|
||||
fire.setAlarmType(info.get("alarmType").asText());
|
||||
fire.setAlarmGrade(info.get("alarmGrade").asText());
|
||||
fire.setAlarmPicture(info.get("alarmPicture").asText());
|
||||
String modifyTime = DateTimeUtil.timeMillisToString(Long.parseLong(info.get("alarmDate").asText()));
|
||||
fire.setAlarmDate(modifyTime);
|
||||
fire.setAlarmSourceName(info.get("alarmSourceName").asText());
|
||||
fire.setAlarmLongitude(info.get("gpsX").asText());
|
||||
fire.setAlarmLatitude(info.get("gpsY").asText());
|
||||
fire.setAltitude(info.get("altitude").asText());
|
||||
fire.setFireDistance(info.get("fireDistance").asText());
|
||||
fire.setFireId(info.get("fsid").asText());
|
||||
Map<String,String> map = new HashMap<String,String>();
|
||||
map.put("deviceCode",info.get("deviceCode").asText());
|
||||
map.put("channelSeq",info.get("channelSeq").asText());
|
||||
map.put("unitType",info.get("unitType").asText());
|
||||
map.put("nodeType",info.get("nodeType").asText());
|
||||
map.put("nodeCode",info.get("nodeCode").asText());
|
||||
map.put("alarmCode",alarmCode);
|
||||
map.put("alarmStat",info.get("alarmStat").asText());
|
||||
map.put("alarmType",info.get("alarmType").asText());
|
||||
map.put("alarmGrade",info.get("alarmGrade").asText());
|
||||
map.put("alarmPicture",info.get("alarmPicture").asText());
|
||||
map.put("alarmDate",modifyTime);
|
||||
map.put("alarmSourceName",info.get("alarmSourceName").asText());
|
||||
map.put("alarmLongitude",info.get("gpsX").asText());
|
||||
map.put("alarmLatitude",info.get("gpsY").asText());
|
||||
map.put("altitude",info.get("altitude").asText());
|
||||
map.put("fireDistance",info.get("fireDistance").asText());
|
||||
map.put("fsid",info.get("fsid").asText());
|
||||
String jsonString = JSON.toJSONString(map);
|
||||
WebSocketServer.BroadCastInfo(jsonString);
|
||||
|
||||
fireDao.save(fire);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 订阅火情图片
|
||||
* @param message
|
||||
*/
|
||||
@Transactional(rollbackOn = Exception.class)
|
||||
@JmsListener(destination="${spring.activemq.topic-name1}", containerFactory="topicListener")
|
||||
public void readActivePicture(String message) throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode jsonNode = objectMapper.readTree(message);
|
||||
String method = jsonNode.get("method").asText();
|
||||
if("vms.notifyAlarmPicture".equals(method)){
|
||||
JsonNode info = jsonNode.get("info");
|
||||
String alarmCode = info.get("alarmCode").asText();
|
||||
Fire byAlarmCode = fireDao.findByAlarmCode(alarmCode);
|
||||
if("".equals(byAlarmCode.getPicturePath()) || byAlarmCode.getPicturePath() == null){
|
||||
String picture = info.get("picture").asText();
|
||||
fireDao.updatePicturePath(alarmCode,picture);
|
||||
}else {
|
||||
String picture1 = byAlarmCode.getPicturePath() + "," + info.get("picture").asText();
|
||||
fireDao.updatePicturePath(alarmCode,picture1);
|
||||
}
|
||||
|
||||
System.out.println("topic接受到的图片:" + message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
26
src/main/java/com/xkrs/util/WeatherUtils.java
Normal file
26
src/main/java/com/xkrs/util/WeatherUtils.java
Normal file
@ -0,0 +1,26 @@
|
||||
package com.xkrs.util;
|
||||
|
||||
/**
|
||||
* @Author: XinYi Song
|
||||
* @Date: 2022/2/7 16:22
|
||||
*/
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description:获取地区天气工具类
|
||||
*/
|
||||
public class WeatherUtils {
|
||||
|
||||
public static JsonNode getForecastWeather() {
|
||||
String url = "http://portalweather.comsys.net.cn/weather03/api/weatherService/getDailyWeather?cityName=大同";
|
||||
Map<String, String> map = new HashMap<>(3);
|
||||
JsonNode jsonNode = RequestUtil.doGetJsonNode(url, map);
|
||||
return jsonNode;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user