jar包下載地址:https://yvioo.lanzous.com/b00nos2le
密碼:383mhtml
maven依賴java
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency>
Message.java 短信實體類api
package com.test; public class Message { private String mobile; private String signName; private String TemplateCode; private String TemplateParam; public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getSignName() { return this.signName; } public void setSignName(String signName) { this.signName = signName; } public String getTemplateCode() { return this.TemplateCode; } public void setTemplateCode(String templateCode) { this.TemplateCode = templateCode; } public String getTemplateParam() { return this.TemplateParam; } public void setTemplateParam(String templateParam) { this.TemplateParam = templateParam; } public Message(String mobile, String signName, String templateCode, String templateParam) { this.mobile = mobile; this.signName = signName; this.TemplateCode = templateCode; this.TemplateParam = templateParam; } public Message() { } }
SendMessageUtil.java
package com.test; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.test.Message; import java.util.Random; public class SendMessageUtil { static final String product = "Dysmsapi"; static final String domain = "dysmsapi.aliyuncs.com"; static final String accessKeyId = ""; static final String accessKeySecret = ""; public static SendSmsResponse sendSms(Message message) throws ClientException { System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setPhoneNumbers(message.getMobile()); request.setSignName(message.getSignName()); request.setTemplateCode(message.getTemplateCode()); request.setTemplateParam(message.getTemplateParam()); SendSmsResponse sendSmsResponse = (SendSmsResponse) acsClient.getAcsResponse(request); return sendSmsResponse; } public static String getSixNum() { String str = "0123456789"; StringBuilder sb = new StringBuilder(4); for (int i = 0; i < 6; ++i) { char ch = str.charAt((new Random()).nextInt(str.length())); sb.append(ch); } return sb.toString(); } }
Test.java 調用方法app
具體的代碼邏輯能夠自行完善dom
@RequestMapping("/sendRegisterMessage") @ResponseBody public void sendMessage(String mobile, HttpServletRequest request, HttpServletResponse response, ModelMap model) { JSONObject object = new JSONObject(); String code = "500"; String msg = ""; if (StringUtils.isBlank(mobile)) { code = "201"; object.put("code", code); ResponseUtils.renderJson(response, "" + object); } else { JSONObject param = new JSONObject(); param.put("code", SendMessageUtil.getSixNum()); System.out.println("註冊短信驗證碼:" + param.getString("code")); Message message = new Message(mobile, "短信簽名", "短信模板ID", "" + param); try { SendSmsResponse sendSmsResponse = SendMessageUtil.sendSms(message); if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) { code = "200"; } else { code = "202"; } msg = sendSmsResponse.getMessage(); } catch (Exception var11) { var11.printStackTrace(); } object.put("code", code); object.put("msg", msg); object.put("msgCode", "" + param.get("code")); ResponseUtils.renderJson(response, "" + object); } }
js頁面方法調用;http://www.javashuo.com/article/p-rtmnvkpp-mr.htmlmaven