应用服务
验证码
短信通知
广告
发布地址
https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi/2.2.1
源码地址
https://github.com/aliyun/aliyun-openapi-java-sdk/tree/master/aliyun-java-sdk-dysmsapi
Maven
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.2.1</version>
</dependency>
JAVA SDK 代码示意图
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.alibaba.fastjson.JSON;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;
public class DescribeImages {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAjVUwKznS*****", "BNPO1zoNSi484oizGM9fzzwJJ*****");
IAcsClient client = new DefaultAcsClient(profile);
// 创建API请求并设置参数
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers("your_value"); // 该参数值为假设值,请您根据实际情况进行填写
request.setSignName("your_value"); // 该参数值为假设值,请您根据实际情况进行填写
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response));
// 打印您需要的返回值,此处打印的是此次请求的 RequestId
System.out.println(response.getRequestId());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
// 打印错误码
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
验证码生成工具
public static Integer generateValidateCode(int length){
Integer code =null;
if(length == 4){
code = new Random().nextInt(9999);//生成随机数,最大为9999
if(code < 1000){
code = code + 1000;//保证随机数为4位数字
}
}else if(length == 6){
code = new Random().nextInt(999999);//生成随机数,最大为999999
if(code < 100000){
code = code + 100000;//保证随机数为6位数字
}
}else{
throw new RuntimeException("只能生成4位或6位数字验证码");
}
return code;
}
public static String generateValidateCode4String(int length){
Random rdm = new Random();
String hash1 = Integer.toHexString(rdm.nextInt());
String capstr = hash1.substring(0, length);
return capstr;
}