You've already forked FrameTour-BE
修改
This commit is contained in:
39
src/main/java/com/ycwl/basic/utils/WxMpUtil.java
Normal file
39
src/main/java/com/ycwl/basic/utils/WxMpUtil.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user