package com.product.text.message.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Service; import com.product.core.config.Global; import com.product.core.exception.BaseException; import com.product.core.message.ISMSService; import com.product.core.spring.context.SpringMVCContextHolder; import com.product.util.config.ErrorCode; /** * 实现短信发送 * 有变更,另写一个新类,实现接口,并去掉老类的实现类,即可 * @version 创建时间:2017年3月27日 下午2:36:08 * */ @Service public class LinKaiSMSUtil implements ISMSService { private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; private static String CorpID="CDJS003057";//账户名 private static String Pwd="zm0513@";//密码 private static String Signature="【成都龙欣软件有限公司】";//签名 德润美家 企业管理平台 成都龙欣科技 static Map error=new HashMap<>(); static{ error.put("-1","账号未注册"); error.put("-2","其他错误:"); error.put("-3","帐号或密码错误"); error.put("-5","余额不足,请充值"); error.put("-6","定时发送时间不是有效的时间格式"); error.put("-7","提交信息末尾未加签名,请添加中文的企业签名【】"); error.put("-8","发送内容需在1到300字之间"); error.put("-9","发送号码为空"); error.put("-10","定时时间不能小于系统当前时间"); error.put("-11","屏蔽手机号码"); error.put("-100","IP黑名单"); error.put("-101","调用接口频率过快(大于30s调用一次)"); error.put("-102","账号黑名单"); error.put("-103","IP未导白"); } /**林凱-發送短信接口*/ /** * */ @Override public int sendSMS(String mobile,String content,String send_time) throws BaseException{ URL url = null; content+=Signature; try { String send_content=URLEncoder.encode(content.replaceAll("
", " "), "GBK");//发送内容 url = new URL("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID="+CorpID+"&Pwd="+Pwd+"&Mobile="+mobile+"&Content="+send_content+"&Cell=&SendTime="+send_time); // url = new URL("https://sdk2.028lk.com/sdk2/SelSum.aspx?CorpID="+CorpID+"&Pwd="+Pwd); // 查询短信剩余条数 BufferedReader in = null; int inputLine = 0; in = new BufferedReader(new InputStreamReader(url.openStream())); inputLine = new Integer(in.readLine()).intValue(); SpringMVCContextHolder.getSystemLogger().info("短信发送:"+content+",返回结果:"+inputLine); if(inputLine<0) { throw new BaseException(""+inputLine,""+error.get(""+inputLine)); } return inputLine; }catch(Exception e) { e.printStackTrace(); throw new BaseException(ErrorCode.MESSAGE_SMS_SEND_FAIL); } } // /**林凱-發送短信接口*/ // @Override // public int sendSMS(String Mobile,String Content,String send_time)throws BaseException { // URL url = null; // Content+=Signature; // String send_content= null;//发送内容 // try { // send_content = URLEncoder.encode(Content.replaceAll("
", " "), "GBK"); // url = new URL("https://sdk2.028lk.com/sdk2/BatchSend2.aspx?CorpID="+CorpID+"&Pwd="+Pwd+"&Mobile="+Mobile+"&Content="+send_content+"&Cell=&SendTime="+send_time); // } catch (UnsupportedEncodingException | MalformedURLException e) { // throw new BaseException("public int sendSMS(String Mobile,String Content,String send_time)throws BaseException",e.getMessage()); // } // System.out.println("url:"+url.to); //// url = new URL("https://sdk2.028lk.com/sdk2/SelSum.aspx?CorpID="+CorpID+"&Pwd="+Pwd); // 查询短信剩余条数 // BufferedReader in = null; // int inputLine = 0; // try { // System.out.println("开始发送短信手机号码为 :"+Mobile); // in = new BufferedReader(new InputStreamReader(url.openStream())); // inputLine = new Integer(in.readLine()).intValue(); // } catch (Exception e) { // e.printStackTrace(); // System.out.println("网络异常,发送短信失败!"); // throw new BaseException("网络异常,发送短信失败!-2",e.getMessage()); // } // System.out.println("结束发送短信返回值: "+inputLine); // return inputLine; // } public String SendSMS(String phoneNum) { int mobile_code = (int)((Math.random()*9+1)*100000); String content = new String("您的验证码是:" + mobile_code + ",请不要把验证码泄露给其他人。"); int result; try { result = sendSMS(phoneNum, content, ""); if (result > 0) { return mobile_code + ""; } } catch (Exception e) { return "-2"; } return result + ""; } public static void main(String[] args) { LinKaiSMSUtil smsUtil = new LinKaiSMSUtil(); System.out.println(smsUtil.SendSMS("18030850875")); } }