Java生成微信小程序二维码
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.http.HttpEntity;import org.springframework.http.HttpMethod;import org.springframework.http.ResponseEntity;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.client.RestTemplate;import com.alibaba.fastjson.JSONObject;import shaded.org.apache.commons.codec.binary.Base64;/** * 微信二维码生成 * @author cq */public class WX_java { /** * (一) 获取 accessToken * @param request * @param response * @return 返回的是JSON类型 ; 获取accessToken:get("access_token"); */public static JSONObject getAccessToken(HttpServletRequest request, HttpServletResponse response) {JSONObject json = null;try {URL url = new URL("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxx");HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();urlcon.connect(); // 获取连接InputStream is = urlcon.getInputStream();BufferedReader buffer = new BufferedReader(new InputStreamReader(is));StringBuffer bs = new StringBuffer();String str = null;while ((str = buffer.readLine()) != null) {bs.append(str);}json = (JSONObject) JSONObject.parse(bs.toString());System.out.println("------------" + json);} catch (Exception e) {e.printStackTrace();}return json;} /** * (二) 二维码生成:通过该接口生成的小程序码,永久有效,数量暂无限制 * @param sceneStr 参数 * @param accessToken 密匙 * @return */public static MapString, Object getminiqrQrTwo(String sceneStr, String accessToken) {RestTemplate rest = new RestTemplate();InputStream inputStream = null;OutputStream outputStream = null;try {String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;MapString, Object param = new HashMapString,Object();param.put("scene", "a1wJgLz0Dcg,sw_001");// 输入参数 最大32字符param.put("page", "pages/index/index");// 路径 如果没有默认跳转到首页面微信小程序发布后才可以使用不能添加参数param.put("width", "430");// 二维码尺寸param.put("is_hyaline", true); // 是否需要透明底色, is_hyaline 为true时,生成透明底色的小程序码 参数仅对小程序码生效param.put("auto_color", false); // 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调 参数仅对小程序码生效// 颜色 auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"}// 十进制表示MapString, Object line_color = new HashMapString,Object();line_color.put("r", 0);line_color.put("g", 0);line_color.put("b", 0);param.put("line_color", line_color);System.out.println("调用生成微信URL接口传参:" + param);MultiValueMapString, String headers = new LinkedMultiValueMapString,String();// 头部信息ListString list = new ArrayListString();list.add("Content-Type");list.add("application/json");headers.put("header", list);HttpEntityObject requestEntity = new HttpEntityObject(param, headers);ResponseEntitybyte[] entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class,new Object[0]);System.out.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());byte[] result = entity.getBody();System.out.println(Base64.encodeBase64String(result));inputStream = new ByteArrayInputStream(result);File file = new File("D:\1.png");// 这里返回的是生成的二维码if (!file.exists()) {file.createNewFile();}outputStream = new FileOutputStream(file);int len = 0;byte[] buf = new byte[1024];while ((len = inputStream.read(buf, 0, 1024)) != -1) {outputStream.write(buf, 0, len);}outputStream.flush();} catch (Exception e) {System.out.println("调用小程序生成微信永久小程序码URL接口异常" + e);} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if (outputStream != null) {try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return null;}// 测试代码public static void getErWeiMa(String access_token) {try {// URL url = new// URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);URL url = new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + access_token);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());// 发送请求参数JSONObject paramJson = new JSONObject(); paramJson.put("scene", "a1wJgLz0Dcg,sw_001");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", 054);lineColor.put("g", 037);lineColor.put("b", 159);paramJson.put("line_color", lineColor);printWriter.write(paramJson.toString());// flush输出流的缓冲printWriter.flush();// 开始获取数据BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());OutputStream os = new FileOutputStream(new File("D:\erweima3.txt"));int len;byte[] arr = new byte[1024];while ((len = bis.read(arr)) != -1) {os.write(arr, 0, len);os.flush();}os.close();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) { HttpServletRequest request = null; HttpServletResponse response = null; //getAccessToken(request, response); getminiqrQrTwo("123456", getAccessToken(request, response).get("access_token").toString()); //getErWeiMa(getAccessToken(request, response).get("access_token").toString()); // getminiqrQrOne(getAccessToken(request, // response).get("access_token").toString()); }}