运作代码
工具类util

package com.cbb.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.springframework.stereotype.Component;
/**
网站 还有这个,国内的接码平台:成本极低,0.1¥单条http://h5.yezi66.net:90/invite/771536先注册一个账号,获得账号密码 需要在文本短信签名模板管理提交认证 获得token与模板Idauthor 陈斌斌

  • @Date 2022年5月11日 14点23分

/
@Component
public class Verification {
/
*
* 发送验证码的接口地址
/
private static final String URL = “http://www.lokapi.cn/smsUTF8.aspx”;
/
*
* 开通服务的账号
/
private static final String USERNAME = “xxxxxxxxxxxx”;
/
*
* 登录密码
/
private static final String PASSWORD = “xxxxxxxxxxxx”;
/
*
* 在文字短信 -产品总览- 验证码TOCKEN
/
private static final String TOCKEN = “xxxxxxxx”;
/
*
* 模板管理-模板Id
/
private static final String ACCOUNTSID = “xxxxxxxx”;
/
*
* 获得当前毫秒数
/
private static final long TIMESTAMP = System.currentTimeMillis();
/
*
* 传一个你要发送验证码的手机号
*
* @param phone
* @return
* @throws ParseException
/
public Map<String, String> verificationCode(String phone) throws ParseException {
String beforSign = “action=sendtemplate&username=” + USERNAME + “&password=” + getMD5String(PASSWORD)
+ “&token=” + TOCKEN + “×tamp=” + TIMESTAMP;
String postData = “action=sendtemplate&username=” + USERNAME + “&password=” + getMD5String(PASSWORD) + “&token=”
+ TOCKEN + “&templateid=” + ACCOUNTSID + “¶m=” + phone + “|” + randomCode()
+ “&rece=json×tamp=” + TIMESTAMP + “&sign=” + getMD5String(beforSign);
Map<String, String> map = new HashMap<String, String>();
try {
sendPost(URL, postData);
map.put(“code”, “200”);
map.put(“randomCode”,String.valueOf(randomCode()));
map.put(“message”, “发送成功”);
return map;
} catch (Exception e) {
map.put(“code”, “400”);
map.put(“message”, “欠费”);
return map;
}
}
/
*
* 生成6位随机数
/
public static int randomCode() {
return (int) ((Math.random() * 9 + 1) * 100000);
}
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = “”;
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty(“accept”, "
/*");
conn.setRequestProperty(“connection”, “Keep-Alive”);
conn.setRequestProperty(“user-agent”, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)”);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(“发送 POST 请求出现异常!” + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static String getMD5String(String rawString) { // 用来计算MD5的函数
String[] hexArray = { “0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “A”, “B”, “C”, “D”, “E”, “F” };
try {
MessageDigest md = MessageDigest.getInstance(“MD5”);
md.update(rawString.getBytes());
byte[] rawBit = md.digest();
String outputMD5 = " ";
for (int i = 0; i < 16; i++) {
outputMD5 = outputMD5 + hexArray[rawBit[i] >>> 4 & 0x0f];
outputMD5 = outputMD5 + hexArray[rawBit[i] & 0x0f];
}
return outputMD5.trim();
} catch (Exception e) {
System.out.println(“计算MD5值发生错误”);
e.printStackTrace();
}
return null;
}
/**
* 生成秘钥
*
* @param tm
* @param key
* @return
/
public static String createSign(TreeMap<String, String> tm, String key) {
StringBuffer buf = new StringBuffer(key);
for (Map.Entry<String, String> en : tm.entrySet()) {
String name = en.getKey();
String value = en.getValue();
if (!“sign”.equals(name) && !“param”.equals(name) && value != null && value.length() > 0
&& !“null”.equals(value)) {
buf.append(name).append(’=’).append(value).append(’&’);
}
}
String _buf = buf.toString();
return _buf.substring(0, _buf.length() - 1);
}
/
*
* 将文件转成base64 字符串
*
* @param path文件路径
* @return *
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
;
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return “”;
}
}

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐