微信小程序獲取二維碼(直接上代碼)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 = "appid=" + ConstantUtil.APP_ID + "&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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM