根据参数生成小程序二维码
生活随笔
收集整理的這篇文章主要介紹了
根据参数生成小程序二维码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
附上微信官方鏈接
/*** @param 用于獲取access_token* @param apiKey 小程序id* @param secretKey 小程序密鑰* @return access_token* @throws Exception*/public String postToken() throws Exception {String apiKey = Conf.getWxAppid();//小程序idString secretKey = Conf.getWxAppScrect();//小程序密鑰String grant_type = "client_credential";String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+apiKey+"&secret="+secretKey;URL url = new URL(requestUrl);// 打開和URL之間的連接HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 設置通用的請求屬性connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Connection", "Keep-Alive");connection.setUseCaches(false);connection.setDoOutput(true);connection.setDoInput(true);// 得到請求的輸出流對象DataOutputStream out = new DataOutputStream(connection.getOutputStream());out.writeBytes("");out.flush();out.close();// 建立實際的連接connection.connect();// 定義 BufferedReader輸入流來讀取URL的響應BufferedReader in = null;if (requestUrl.contains("nlp"))in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));elsein = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String result = "";String getLine;while ((getLine = in.readLine()) != null) {result += getLine;}in.close();JSONObject jsonObject = JSON.parseObject(result);String accesstoken=jsonObject.getString("access_token");return accesstoken;}/*** 生成帶參小程序二維碼* @param scene 要輸入的內容* @param accessToken token*/public String postMiniqrQr(String scene) {String encode = null;try{String accessToken = this.postToken();URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();// conn.setConnectTimeout(10000);//連接超時 單位毫秒// conn.setReadTimeout(2000);//讀取超時 單位毫秒// 發送POST請求必須設置如下兩行httpURLConnection.setDoOutput(true); // 打開寫入屬性httpURLConnection.setDoInput(true); // 打開讀取屬性httpURLConnection.setRequestMethod("POST");// 提交方式// 不得不說一下這個提交方式轉換!!真的坑。。改了好長時間!!一定要記得加響應頭httpURLConnection.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8");// 設置響應頭// 獲取URLConnection對象對應的輸出流PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());// 發送請求參數JSONObject paramJson = new JSONObject();paramJson.put("scene", scene); // 你要放的內容paramJson.put("path", Conf.getWxAppPath());paramJson.put("width", 430); // 寬度paramJson.put("auto_color", true);paramJson.put("is_hyaline", true);printWriter.write(paramJson.toString());// flush輸出流的緩沖printWriter.flush();BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());if (bis != null) {byte[] buffer = new byte[1024];//每次讀取的字符串長度,如果為-1,代表全部讀取完畢int len;ByteArrayOutputStream outStream = new ByteArrayOutputStream();while ((len = bis.read(buffer)) != -1) {outStream.write(buffer, 0, len);}bis.close();byte[] bytes = outStream.toByteArray(); // wxQrCode.read(bytes);BASE64Encoder base64Encoder = new BASE64Encoder();encode = base64Encoder.encode(bytes);}return encode; // //創建一個空文件 // OutputStream os = new FileOutputStream(new File("E:/test/test3.jpg")); // //ByteArrayOutputStream os = new ByteArrayOutputStream(); // int len; // byte[] arr = new byte[1024]; // while ((len = bis.read(arr)) != -1) // { // os.write(arr, 0, len); // os.flush(); // } // os.close();// 上傳云儲存//InputStream is = new ByteArrayInputStream(os.toByteArray());//retMap = UploadUtils.upload(is);}catch (Exception e){e.printStackTrace();}return encode;}在官方文檔寫的很清楚scene只能是String類型,所以和前端要商量好傳輸格式
總結
以上是生活随笔為你收集整理的根据参数生成小程序二维码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决 rsync IO input /
- 下一篇: dcs服务器性能指标,ECS700组态及