微信小程序码的生成(JAVA完整版) 亲测可用

JAVA生成小程序码(太阳码)

首先准备工具类,这里我使用的是QrUtil;废话不多说,上工具类;

工具类是获取token使用;

appid = 小程序appID
secret = 小程序秘钥

/*** @author : cph* @Email :540826312@qq.com* @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 = "https://api.weixin.qq.com/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 DataOutputStream(connection.getOutputStream());out.writeBytes("");out.flush();out.close();// 建立实际的连接connection.connect();// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = null;if (requestUrl.contains("nlp"))in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));elsein = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String result = "";String getLine;while ((getLine = in.readLine()) != null) {result += getLine;}in.close();JSONObject jsonObject = JSON.parseObject(result);String accesstoken=jsonObject.getString("access_token");return accesstoken;}}

生成小程序二维码接口
通过生成的小程序码转换成流返回给前端即可

@Overridepublic String getminiqrQr(String sceneStr, String accessToken) {try {URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setRequestMethod("POST");// 提交模式// 发送POST请求必须设置如下两行httpURLConnection.setDoOutput(true);httpURLConnection.setDoInput(true);// 获取URLConnection对象对应的输出流PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());// 发送请求参数JSONObject paramJson = new JSONObject();paramJson.put("scene", sceneStr);paramJson.put("path", null);paramJson.put("width", 430);paramJson.put("auto_color", true);printWriter.write(paramJson.toString());// flush输出流的缓冲printWriter.flush();//开始获取数据//            OutputStream os = new FileOutputStream(new File("C:/Users/Administrator/Desktop/1.png"));try (InputStream is = httpURLConnection.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();){byte[] buffer = new byte[1024];int len = -1;while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}return Base64.getEncoder().encodeToString(baos.toByteArray());}} catch (Exception e) {e.printStackTrace();}return null;}

到此为止,生成小程序码已完成,亲测可用

Published by

风君子

独自遨游何稽首 揭天掀地慰生平