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;
}