工具类--发送验证码短信

SmsController层
@Slf4j
@RestController
@RequestMapping("/third/allincloud/sms")
public class SmsController {

private Logger logger = LoggerFactory.getLogger(SmsController.class);

@Autowired
SmsService smsService;

@ApiOperation(value = "发送验证码短信", notes = "发送验证码短信")
@ApiImplicitParams({
@ApiImplicitParam(name = "phone", paramType = "query", value = "会员手机号", dataType = "String"),
@ApiImplicitParam(name = "message", paramType = "query", value = "手机验证码", dataType = "String")
})
@GetMapping(value = "sendSms")
public int sendSms(
@RequestParam(value = "phone", required = true) String phone,
@RequestParam(value = "message", required = true) String message
) throws Exception {
return smsService.sendSms(Const.SEND_SMS_USERNAME, Const.SEND_SMS_PASSWORD, Const.SEND_SMS_URL, phone, message);
}

}

service层
@Service
@Transactional(rollbackFor = Exception.class)
public class SmsServiceImpl implements SmsService{

@Override
public int sendSms(String smsUserName, String smsPassword, String smsUrl, String phone, String message) throws Exception {
return SendSms.sendMessage(smsUserName, smsPassword, smsUrl, phone, message);
}
}

util类
@Slf4j
public class SendSms {

private static final Logger logger = LoggerFactory.getLogger(SendSms.class);
public static int ERROR_SUCCESS = 0; //发送成功
public static int ERROR_USERNAME = -1; //用户帐号不存在 用户账户需要管理员在短信平台上添加
public static int ERROR_PASSWORD = -2; //密码错误
public static int ERROR_CAPTION = -3; //主题不能为空
public static int ERROR_CONTENT = -4; //内容有误 内容为空或大于280个字
public static int ERROR_MOBILE = -5; //手机长度有误 手机号码个数为0或超过1000个
public static int ERROR_TIME_FORMAT = -6; //定时时间错误 时间格式有误(2012-09-15 17:37:00)
public static int ERROR_NO_MONEY = -7; //余额不足
public static int ERROR_OTHER = -8; //未知错误
public static int ERROR_RRID = -9; //Rrid长度有误 不能超过16位
public static int ERROR_AUTHORITY = -10; //没有权限
public static int ERROR_SERVER_ERROR = 5; //短信服务器通信失败
public static int ERROR_URL_ERROR = 10; //请求地址错误;

public static int sendMessage(String username,String password,String sendurl,String mobile, String message) throws Exception {
StringBuilder commander = new StringBuilder();
String rrid = new Random(System.nanoTime()).nextInt() + "";
logger.debug("短信标识:{}-{}", mobile, rrid);
commander.append(String.format("%s?username=%s&password=%s&ext=&schtime=&rrid=%s", sendurl, username, password, rrid));
commander.append(String.format("&mobile=%s&content=%s", mobile, URLEncoder.encode(message, "utf-8")));

logger.debug("短信请求地址:{}-{}", mobile, commander.toString());
int nRet = ERROR_SUCCESS;
URL url = null;
HttpURLConnection urlConn = null;
try {
url = new URL(commander.toString());
urlConn = (HttpURLConnection) url.openConnection();
urlConn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuffer returnContent = new StringBuffer();
String c = null;
while ((c = reader.readLine()) != null) {
returnContent.append(c);
}
reader.close();
String result = returnContent.toString().replaceAll("<[^>]*>", "");
logger.debug("短信发送结果:", mobile, rrid, result);
if (rrid.equals(result)) {
nRet = 0;
}
} catch (MalformedURLException e) {
log.error(e.getMessage(), e);
nRet = ERROR_URL_ERROR;
} catch (Exception e) {
log.error(e.getMessage(), e);
nRet = ERROR_SERVER_ERROR;
} finally {
if (urlConn != null) {
urlConn.disconnect();
}
}
return nRet;
}
// public static void main(String[] args){
// String username=Configuration.SEND_SMS_USERNAME;
// String password=Configuration.SEND_SMS_PASSWORD;
// String sendurl=Configuration.SEND_SMS_URL;
// String mobile="18201039586";
// int smscode = (int)((Math.random()*9+1)*100000);
// String message="您的验证码是:"+smscode+",请不要把验证码泄露给其他人!";
// try {
// int re=sendMessage(username,password,sendurl,mobile,message);
// System.out.println(re);
// }catch (Exception e){
// log.error(e.getMessage(), e);
// }
// }
}
posted @ 2019-05-27 16:02  有容奶大  阅读(277)  评论(0编辑  收藏  举报