Java 對接 SMS簡訊介面
Utile 檔案
//包名 以下內容詳細引數參照文件
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.hnshengdun.hcp_taian.common.data.entity.SendReq;
import com.hnshengdun.hcp_taian.common.data.entity.SendRes;
import org.apache.commons.codec.binary.Base64;
import com.alibaba.fastjson.JSON;
public class SMSUtils {
private static String apId=""; private static String secretKey=""; private static String ecName = ""; //集團名稱 private static String sign = ""; //閘道器簽名編碼 private static String addSerial = ""; //拓展碼 填空 public static String url = "http://112.35.1.155:1992/sms/norsubmit";//請求url /** * 多使用者傳送簡訊資訊 * @param mobiles 手機號碼逗號分隔 * @param content 簡訊內容 * @return 返回1表示成功,0表示失敗 * @throws IOException */ public static int sendMsg(String mobiles,String content) throws IOException{ Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowDatestr = sdf.format(calendar.getTimeInMillis()); content += nowDatestr; //簡訊內容後跟個日期時間(可有可無),需求要求 SendReq sendReq = new SendReq(); sendReq.setApId(apId); sendReq.setEcName(ecName); sendReq.setSecretKey(secretKey); sendReq.setContent(content); sendReq.setMobiles(mobiles); sendReq.setAddSerial(addSerial); sendReq.setSign(sign); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(sendReq.getEcName()); stringBuffer.append(sendReq.getApId()); stringBuffer.append(sendReq.getSecretKey()); stringBuffer.append(sendReq.getMobiles()); stringBuffer.append(sendReq.getContent()); stringBuffer.append(sendReq.getSign()); stringBuffer.append(sendReq.getAddSerial()); //System.out.println(stringBuffer.toString()); sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase()); //System.out.println(sendReq.getMac()); String reqText = JSON.toJSONString(sendReq); String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8")); //System.out.println(encode); String resStr = sendPost(url,encode); System.out.println("傳送簡訊結果:"+resStr); SendRes sendRes = JSON.parseObject(resStr,SendRes.class); if(sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())){ return 1; }else{ return 0; } }
/*
//main方法測試傳送簡訊,返回1表示成功,0表示失敗
public static void main(String[] args) throws IOException{
String msg = "這是傳送簡訊的內容!";
int result = sendMsg("183xxxx65xx,183xxxx12xx",msg);
System.out.println("==="+result);
}*/
/** * 向指定 URL 傳送POST方法的請求 * * @param url * 傳送請求的 URL * @param param * 請求引數 * @return 所代表遠端資源的響應結果 */ private static String sendPost(String url, String param) { OutputStreamWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("contentType","utf-8"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new OutputStreamWriter(conn.getOutputStream()); out.write(param); out.flush(); in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }
}
MD5 檔案
//包名別忘記
import java.security.MessageDigest;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
/**
- MD5加密/驗證工具類
- @author bluesky
*/
public class Md5Util {
static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
/**
* 生成MD5碼
*
* @param plainText
* 要加密的字串
* @return md5值
*/
public final static String MD5(String plainText) {
try {
byte[] strTemp = plainText.getBytes("UTF-8");
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
/**
* 生成MD5碼
*
* @param plainText
* 要加密的字串
* @return md5值
*/
public final static String MD5(byte[] plainText) {
try {
byte[] strTemp = plainText;
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
return null;
}
}
/**
* 先進行HmacSHA1轉碼再進行Base64編碼
* @param data 要SHA1的串
* @param key 祕鑰
* @return
* @throws Exception
*/
public static String HmacSHA1ToBase64(String data, String key) throws Exception {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
byte[] rawHmac = mac.doFinal(data.getBytes());
return Base64.encodeBase64String(rawHmac);
}
/**
* 校驗MD5碼
*
* @param text
* 要校驗的字串
* @param md5
* md5值
* @return 校驗結果
*/
public static boolean valid(String text, String md5) {
return md5.equals(MD5(text)) || md5.equals(MD5(text).toUpperCase());
}
/**
*
* @param params
* @return
*/
public static String MD5(String... params) {
StringBuilder sb = new StringBuilder();
for (String param : params) {
sb.append(param);
}
return MD5(sb.toString());
}
}
Service 處理層
Map<String,String> contentMap = new HashMap<>();
for (int i = 0; i < epp.size(); i++) {
Person_data pda = pd.getByid(epp.get(i).getPerson_data_id());
Eva_person ep = new Eva_person();
ep.setEva_project_person_id(epp.get(i).getId());
ep.setAccount(pda.getS_name());
pwd = AcounntUtil.getStringRandom(8);
ep.setPwd(pwd);
ep.setEva_ass_id(id);
ep.setPerson_data_id(epp.get(i).getPerson_data_id());
int b = aep.addPerson(ep);
contentMap.put(pda.getMob(),"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
if (b != 1) {
throw new Exception();
}
rele.setStep(3);
rele.setId(id);
int a = evas.editEvaassById(rele);
if (a != 1) {
throw new Exception();
}
}
String content = JSON.toJSONString( contentMap );
int a= SMSUtils.sendMsg("",content);
if (a==1){
return 1;
}else {
return 2;
}