⼩程序码的⽣成(JAVA完整版)亲测可⽤
JAVA⽣成⼩程序码(太阳码)
⾸先准备⼯具类,这⾥我使⽤的是QrUtil;废话不多说,上⼯具类;
⼯具类是获取token使⽤;
appid = ⼩程序appID
secret = ⼩程序秘钥
/**
* @author : cph
* @Email :540826312@qq
* @Date :2020-07-04 9:27
*/
@Component
public class QrUtil {
@Value("${wechat.appid}")
private static String API_KEY;
@Value("${wechat.secret}")
private static String SECRET;
public static String getApiKey(){
return API_KEY;
}
public void setApiKey(String apiKey){
API_KEY = apiKey;
}
public static String getSECRET(){
return SECRET;
}
public void setSECRET(String SECRET){
QrUtil.SECRET = SECRET;
}
public static String postToken()throws Exception {
String requestUrl ="api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid="+ API_KEY +"&secret="+SECRET; URL url =new URL(requestUrl);
// 打开和URL之间的连接
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通⽤的请求属性
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("Connection","Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out =new OutputStream());
out.writeBytes("");
out.flush();
out.close();
// 建⽴实际的连接
// 定义 BufferedReader输⼊流来读取URL的响应
BufferedReader in = null;
ains("nlp"))
in =new BufferedReader(new InputStream(),"GBK"));
else
in =new BufferedReader(new InputStream(),"UTF-8"));
String result ="";
String getLine;
有趣的java小程序while((getLine = in.readLine())!= null){
result += getLine;
}
in.close();
JSONObject jsonObject = JSON.parseObject(result);
String String("access_token");
return accesstoken;
}
}
⽣成⼩程序⼆维码接⼝
通过⽣成的⼩程序码转换成流返回给前端即可
@Override
public String getminiqrQr(String sceneStr, String accessToken){
try{
URL url =new URL("api.weixin.qq/wxa/getwxacodeunlimit?access_token="+ accessToken); HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// 发送POST请求必须设置如下两⾏
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter =new OutputStream());
// 发送请求参数
JSONObject paramJson =new JSONObject();
paramJson.put("scene", sceneStr);
paramJson.put("path", null);
paramJson.put("width",430);
paramJson.put("auto_color",true);
printWriter.String());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
// OutputStream os = new FileOutputStream(new File("C:/Users/Administrator/Desktop/1.png"));
try(InputStream is = InputStream();
ByteArrayOutputStream baos =new ByteArrayOutputStream();){
byte[] buffer =new byte[1024];
int len =-1;
while((len = is.read(buffer))!=-1){
baos.write(buffer,0, len);
}
Encoder().ByteArray());
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
到此为⽌,⽣成⼩程序码已完成,亲测可⽤
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论