java获取b类小程序码并生成图片

小程序必须已发布

package com.vispractice.focus.union.util;
import java.io.BufferedInputStream;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GetTwoCode {
public static String getToken(String appid,String secret) {
StringBuilder token = new StringBuilder("");
JSONObject jsonObject = null;
BufferedReader bis = null;
PrintWriter printWriter = null;
try {

        URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");// 提交模式
        // conn.setConnectTimeout(10000);//连接超时 单位毫秒
        // conn.setReadTimeout(2000);//读取超时 单位毫秒
        // 发送POST请求必须设置如下两行
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        printWriter = new PrintWriter(httpURLConnection.getOutputStream());
        // 发送请求参数
        JSONObject paramJson = new JSONObject();
 /*       paramJson.put("scene", "a=1234567890");
        paramJson.put("page", "pages/index/index");
        paramJson.put("width", 430);
        paramJson.put("auto_color", true);*/
        /**
         * line_color生效
         * paramJson.put("auto_color", false);
         * JSONObject lineColor = new JSONObject();
         * lineColor.put("r", 0);
         * lineColor.put("g", 0);
         * lineColor.put("b", 0);
         * paramJson.put("line_color", lineColor);
         * */
        printWriter.write(paramJson.toString());
        // flush输出流的缓冲
        printWriter.flush();
        //开始获取数据
        bis = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
       // OutputStream os = new FileOutputStream(new File(path));
    
        String len;
        while ((len = bis.readLine()) != null) {
        	System.out.println(len);
           token.append(len);
        }
        jsonObject = JSON.parseObject(token.toString());
       
       
    } catch (Exception e) {
       log.error(e.getMessage());
    }finally {
    	try {
			bis.close();
			printWriter.close();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
    }
    return  jsonObject.get("access_token").toString();
}
/**
 * 生成小程序码
 * @param accessToken 
 * @param path 生成小程序码png路径
 */
public static void getCode(String accessToken,String scene,String path) {
	BufferedInputStream bis = null;
	PrintWriter printWriter = null;
	OutputStream os = null;
    try {
        URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");// 提交模式
        // conn.setConnectTimeout(10000);//连接超时 单位毫秒
        // conn.setReadTimeout(2000);//读取超时 单位毫秒
        // 发送POST请求必须设置如下两行
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        // 获取URLConnection对象对应的输出流
       printWriter = new PrintWriter(httpURLConnection.getOutputStream());
        // 发送请求参数
        JSONObject paramJson = new JSONObject();
        paramJson.put("scene", scene);
        paramJson.put("page", "pages/index/index");
        paramJson.put("width", 430);
        paramJson.put("auto_color", true);
        /**
         * line_color生效
         * paramJson.put("auto_color", false);
         * JSONObject lineColor = new JSONObject();
         * lineColor.put("r", 0);
         * lineColor.put("g", 0);
         * lineColor.put("b", 0);
         * paramJson.put("line_color", lineColor);
         * */

        printWriter.write(paramJson.toString());
        // flush输出流的缓冲
        printWriter.flush();
        //开始获取数据
        bis = new BufferedInputStream(httpURLConnection.getInputStream());
        os = new FileOutputStream(new File(path));
        int len;
        byte[] arr = new byte[1024];
        while ((len = bis.read(arr)) != -1) {
            os.write(arr, 0, len);
            os.flush();
        }
      
    } catch (Exception e) {
       log.error(e.getMessage());
    }finally {
    	try {
			os.close();
			bis.close();
			printWriter.close();
		} catch (IOException e) {
			 log.error(e.getMessage());
		}
    
    }
}
public static void main(String[] args) {
	getCode(getToken("appid", "sercet"),"scene", "filepath");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值