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

  • Post author:
  • Post category: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"));
        else
            in = 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;
    }

}


生成小程序二维码接口

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

@Override
    public 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;
    }


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



版权声明:本文为weixin_45593950原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。