一、获取token值
public static Map<String, Object> getToken() {
Map<String, Object> data = new HashMap<String, Object>();
try {
StringBuilder urlSb = new StringBuilder();
urlSb.append(“https://api.weixin.qq.com/cgi-bin/token”);
urlSb.append("?");
urlSb.append(“grant_type=%s&appid=%s&secret=%s”);
String tokenUrl = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=小程序值&secret=小程序的值”;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(tokenUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String resultText = EntityUtils.toString(httpEntity, “utf-8”);
@SuppressWarnings(“unchecked”)
Map<Object, Object> resultMap = JSON.parseObject(resultText, Map.class);
if (resultMap != null) {
if (resultMap != null && resultMap.size() > 0) {
// 合并2次调用结果
Set keySet = resultMap.keySet();
for (Object key : keySet) {
if (!data.containsKey(key)) {
data.put((String) key, resultMap.get(key));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
二、调用token并绑定token同时设置基础参数
public static String StringUtil(Long id){
Map<String, Object> date= getToken();
String url=“https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+date.get("access_token”);
HashMap<String, String> params = new HashMap<>();
params.put(“scene”, “id=”+id+"");
params.put(“page”, “pages/goods/detail/index”);
JSONObject json=JSONObject.fromObject(params);
String val = “”;
Random random = new Random();
for (int i = 0; i < 5; i++) {
val += String.valueOf(random.nextInt(10));
}
wxPost(url,json,val);
return GetImageStr(“d:/”+ val + “.png”);
}
参数scene为添加,page为小程序路径
三、写入图片
//第二步写入图片
public static void wxPost(String uri, JSONObject paramJson, String fileName) {
try {
URL url = new URL(uri);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod(“POST”);// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
// 开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
File file=new File(“d:/”+ fileName + “.png”);// /Users/shenzhuan/logs/
OutputStream os = new FileOutputStream(file);
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
四、将图片转换为base64
public static String GetImageStr(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
InputStream in = null;
byte[] data = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);// 返回Base64编码过的字节数组字符串
}
注意:生成的图片为1kb是,是小程序返回的错误
查看方式:删除后缀.png然后用开发工具打开会显示错误状态码,根据状态码查询具体错误