微信小程序获取二维码,直接上代码https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

应为是直接返回二进制数据所有与其他接口些许差别,希望能帮助现在的你!

微信小程序获取二维码,直接上代码https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN

          谢谢!!!

/**

* 37、微信二维码生成

*/

public String getWeiXinCourseMap() {

  String courseId = StringUtils.defaultString(getPara("courseId"));

  String codeUrl = "";

  String path = "你的二维码指向路径(可以拼接参数)";

  try {

    codeUrl = GetUserOpenId.getCourseMap(path+courseId, 450);

  } catch (Exception e) {

  }

  return codeUrl; //二维码文件下载路径

}

/**

* @方法名:获取小程序二维码

* @参数:path:路径加上参数 width:宽

* @输出: result 文件下载路径

* @备注:

* @作者: 林

* @时间: 2019年12月1 1:15:30

* @修改:

*/

public static String getCourseMap(String path , Integer width){

String assessToken = getAccess_token();

String weixin_url ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+assessToken;

JSONObject js = new JSONObject();

js.put("access_token", assessToken);

js.put("path", path);

js.put("width", width);

String result = CommonUtil.httpsRequestToMap(weixin_url, "POST", js.toJSONString());

Map<String, String> map = new Hashtable<String, String>();

return result;

}

/**

* @方法名:获取access_token

* @参数:

* @输出:

* @备注:

* @作者: 林

* @时间: 2019年12月1 1:21:46

* @修改:

*/

public static String getAccess_token(){

String access_token = "";

String grant_type = "client_credential";

String weixin_url ="https://api.weixin.qq.com/cgi-bin/token";

String params = "app&secret=你的密钥&grant_type=" + grant_type;

String result = CommonUtil.httpsRequest(weixin_url, "POST", params);

try {

//解析相应内容(转换成json对象)

JSONObject json =JSONObject.parseObject(result);

//操作标识(access_token)

access_token =String.valueOf(json.get("access_token"));

} catch (Exception e) {

}

return access_token;

}

/**

* 发送https请求 返回图片

*

* @param requestUrl

* 请求地址

* @param requestMethod

* 请求方式(GET、POST)

* @param outputStr

* 提交的数据

* @return 返回微信服务器响应的信息

*/

public static String httpsRequestToMap(String requestUrl, String requestMethod, String outputStr) {

try {

// 创建SSLContext对象,并使用我们指定的信任管理器初始化

TrustManager[] tm = { new MyX509TrustManager() };

SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");

sslContext.init(null, tm, new java.security.SecureRandom());

// 从上述SSLContext对象中得到SSLSocketFactory对象

SSLSocketFactory ssf = sslContext.getSocketFactory();

URL url = new URL(requestUrl);

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setSSLSocketFactory(ssf);

conn.setDoOutput(true);

conn.setDoInput(true);

conn.setUseCaches(false);

// 设置请求方式(GET/POST)

conn.setRequestMethod(requestMethod);

conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");

// 当outputStr不为null时向输出流写数据

if (null != outputStr) {

OutputStream outputStream = conn.getOutputStream();

// 注意编码格式

outputStream.write(outputStr.getBytes("UTF-8"));

outputStream.close();

}

// 从输入流读取返回内容

long start = System.currentTimeMillis();

InputStream inputStream = conn.getInputStream();

String interUrl = 需要存储的路径+文件名称.png";

FileOutputStream fos = new FileOutputStream(interUrl);

// 一次读取一个字节

int by = 0;

while ((by = inputStream.read()) != -1) {

fos.write(by);

}

// 释放资源

fos.close();

long end = System.currentTimeMillis();

long gong = end - start;

inputStream.close();

inputStream = null;

conn.disconnect();

return interUrl;

} catch (ConnectException ce) {

log.error("连接超时:{}", ce);

} catch (Exception e) {

log.error("https请求异常:{}", e);

}

return null;

}