This commit is contained in:
2024-12-20 17:34:25 +08:00
parent bf5fdeb95c
commit 43ae10916c
29 changed files with 325 additions and 58 deletions

View File

@ -0,0 +1,39 @@
package com.ycwl.basic.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.FileOutputStream;
public class WxMpUtil {
private static final String GET_WXA_CODE_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s";
public static void generateWXAQRCode(String accessToken, String path, String filePath) throws Exception {
String url = String.format(GET_WXA_CODE_URL, accessToken);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject json = new JSONObject();
json.put("path", path);
json.put("width", 430);
StringEntity entity = new StringEntity(json.toJSONString(), "utf-8");
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json");
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
byte[] bytes = EntityUtils.toByteArray(responseEntity);
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(bytes);
}
}
}
}
}