4 changed files with 186 additions and 5 deletions
@ -0,0 +1,182 @@
|
||||
package com.biutag.lan.util; |
||||
|
||||
import com.alibaba.fastjson2.JSON; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.io.BufferedReader; |
||||
import java.io.InputStreamReader; |
||||
import java.io.OutputStreamWriter; |
||||
import java.io.PrintWriter; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.net.URLEncoder; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Base64; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 内网信息平台工具类(未测试) |
||||
*/ |
||||
@Slf4j |
||||
public class SmsLan { |
||||
private static final String BASE_URL = "http://10.143.55.119:8989/kezansms/SendSms"; |
||||
private static final String USERNAME = "fzzd"; |
||||
private static final String PASSWORD = "fzzd12345678dxpt"; |
||||
|
||||
private SmsLan() { |
||||
} |
||||
|
||||
/** |
||||
* @param mobiles 手机号 多个用","隔开 (英文逗号)【必填】 |
||||
* @param content 短信内容 【必填】 |
||||
* @param orderNo 批次号 【必填】 |
||||
* @param appendID 扩展字号 【非必填】 |
||||
* @param sendTime 拟发送时间 【非必填】 |
||||
* @param validTime 拟发送最后时间(超过不发送) 【非必填】 |
||||
* @return |
||||
*/ |
||||
public static String send(String mobiles, String content, String orderNo, String appendID, String sendTime, String validTime) { |
||||
try { |
||||
Map<String, Object> dataMap = new HashMap<>(); |
||||
long timeStamp = System.currentTimeMillis() / 1000; |
||||
dataMap.put("timeStamp", timeStamp); |
||||
log.info("timeStamp: {}", timeStamp); |
||||
dataMap.put("mobiles", mobiles); |
||||
dataMap.put("content", content); |
||||
dataMap.put("orderNo", orderNo); |
||||
dataMap.put("appendID", appendID); |
||||
dataMap.put("sendTime", sendTime); |
||||
dataMap.put("validTime", validTime); |
||||
|
||||
String jsonStr = JSON.toJSONString(dataMap); |
||||
log.info("jsonStr: {}", jsonStr); |
||||
String key = encrypt(jsonStr, PASSWORD, true); |
||||
log.info("key: {}", key); |
||||
key = URLEncoder.encode(key, "UTF-8"); |
||||
log.info("key: {}", key); |
||||
String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); |
||||
log.info(response); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* @param mobiles 手机号 多个用","隔开 (英文逗号)【必填】 |
||||
* @param content 短信内容 【必填】 |
||||
* @param orderNo 批次号 【必填】 |
||||
* @param validTime 拟发送最后时间(超过不发送) 【非必填】 |
||||
* @return |
||||
*/ |
||||
public static String send(String mobiles, String content, String orderNo, String validTime) { |
||||
try { |
||||
Map<String, Object> dataMap = new HashMap<>(); |
||||
long timeStamp = System.currentTimeMillis() / 1000; |
||||
dataMap.put("timeStamp", timeStamp); |
||||
log.info("timeStamp: {}", timeStamp); |
||||
dataMap.put("mobiles", mobiles); |
||||
dataMap.put("content", content); |
||||
dataMap.put("orderNo", orderNo); |
||||
dataMap.put("validTime", validTime); |
||||
|
||||
String jsonStr = JSON.toJSONString(dataMap); |
||||
log.info("jsonStr: {}", jsonStr); |
||||
String key = encrypt(jsonStr, PASSWORD, true); |
||||
log.info("key: {}", key); |
||||
key = URLEncoder.encode(key, "UTF-8"); |
||||
log.info("key: {}", key); |
||||
String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); |
||||
log.info(response); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
/** |
||||
* @param mobiles 手机号 多个用","隔开 (英文逗号)【必填】 |
||||
* @param content 短信内容 【必填】 |
||||
* @param orderNo 批次号 【必填】 |
||||
* @return |
||||
*/ |
||||
public static String send(String mobiles, String content, String orderNo) { |
||||
try { |
||||
Map<String, Object> dataMap = new HashMap<>(); |
||||
long timeStamp = System.currentTimeMillis() / 1000; |
||||
dataMap.put("timeStamp", timeStamp); |
||||
log.info("timeStamp: {}", timeStamp); |
||||
dataMap.put("mobiles", mobiles); |
||||
dataMap.put("content", content); |
||||
dataMap.put("orderNo", orderNo); |
||||
|
||||
String jsonStr = JSON.toJSONString(dataMap); |
||||
log.info("jsonStr: {}", jsonStr); |
||||
String key = encrypt(jsonStr, PASSWORD, true); |
||||
log.info("key: {}", key); |
||||
key = URLEncoder.encode(key, "UTF-8"); |
||||
log.info("key: {}", key); |
||||
String response = httpUtilPost(BASE_URL, "userid=" + USERNAME + "&key=" + key); |
||||
log.info(response); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* POST 请求 |
||||
* |
||||
* @param urls |
||||
* @param params |
||||
* @return |
||||
*/ |
||||
public static String httpUtilPost(String urls, String params) { |
||||
URL url; |
||||
HttpURLConnection uConn = null; |
||||
String result = ""; |
||||
try { |
||||
url = new URL(urls); |
||||
uConn = (HttpURLConnection) url.openConnection(); |
||||
uConn.setRequestMethod("POST"); |
||||
uConn.setDoInput(true); |
||||
uConn.setDoOutput(true); |
||||
uConn.connect(); |
||||
try (PrintWriter out = new PrintWriter(new OutputStreamWriter(uConn.getOutputStream(), "utf-8")); |
||||
BufferedReader in = new BufferedReader(new InputStreamReader(uConn.getInputStream(), "utf-8"))) { |
||||
out.write(params); |
||||
out.flush(); |
||||
StringBuilder resultBuilder = new StringBuilder(); |
||||
String line; |
||||
while ((line = in.readLine()) != null) { |
||||
if (line.length() > 0) |
||||
resultBuilder.append(line); |
||||
} |
||||
result = resultBuilder.toString(); |
||||
} |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
if (uConn != null) { |
||||
uConn.disconnect(); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
|
||||
public static String encrypt(String input, String key, Boolean bs64) throws Exception { |
||||
SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); |
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, skey); |
||||
byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
if (bs64) { |
||||
return Base64.getEncoder().encodeToString(crypted); |
||||
} else { |
||||
throw new UnsupportedOperationException("Non-base64 is not supported."); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue