spring boot微信接口生成小程序二維碼

多借鑒網(wǎng)上大神的代碼和微信開發(fā)文檔

獲取token根據(jù)token獲取二維碼CreateQrcore?

注意:這里的微信小程序信息要為已上線的小程序臭增。

public class CreateQrcore {

/*

* 獲取 token

* 普通的 get 可獲 token

*/

? ? public? static StringgetToken() {

try {

Map map =new LinkedHashMap<>();

? ? ? ? ? ? map.put("grant_type", "client_credential");

? ? ? ? ? ? map.put("appid","wxfa188a42d843a0b0");

? ? ? ? ? ? map.put("secret", "0433593dd1887ea5381e6d01308f81ba");

? ? ? ? ? ? String rt = UrlUtil.sendPost("https://api.weixin.qq.com/cgi-bin/token", map);

? ? ? ? ? ? System.out.println("what is:"+rt);

? ? ? ? ? ? JSONObject json = JSONObject.fromObject(rt);

? ? ? ? ? ? if (json.getString("access_token") !=null || json.getString("access_token") !="") {

return json.getString("access_token");

? ? ? ? ? ? }else {

return null;

? ? ? ? ? ? }

}catch (Exception e) {

e.printStackTrace();

return null;

? ? ? ? }

}

/*

* 獲取 二維碼圖片

*

*/

? ? public static StringgetminiqrQr( String accessToken,HttpServletRequest request,HttpServletResponse response,Integer id)throws Exception{

String p="D://code"; //二維碼生產(chǎn)的地址? 本地F盤code文件夾

? ? ? ? System.out.println(p);

? ? ? ? String codeUrl=p+"/twoCode.png";

? ? ? ? String twoCodeUrl="twoCode.png";

? ? ? ? ? ? URL url =new URL("https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="+accessToken);

? ? ? ? ? ? HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

? ? ? ? ? ? httpURLConnection.setRequestMethod("POST");// 提交模式

// conn.setConnectTimeout(10000);//連接超時 單位毫秒

// conn.setReadTimeout(2000);//讀取超時 單位毫秒

// 發(fā)送POST請求必須設(shè)置如下兩行

? ? ? ? ? ? httpURLConnection.setDoOutput(true);

? ? ? ? ? ? httpURLConnection.setDoInput(true);

? ? ? ? ? ? // 獲取URLConnection對象對應(yīng)的輸出流

? ? ? ? ? ? PrintWriter printWriter =new PrintWriter(httpURLConnection.getOutputStream());

? ? ? ? ? ? // 發(fā)送請求參數(shù)

? ? ? ? ? ? JSONObject paramJson =new JSONObject();

//? ? ? ? ? ? paramJson.put("scene","2");//這就是你二維碼里攜帶的參數(shù) String型? 名稱不可變

? ? ? ? ? ? paramJson.put("path", "pages/order/clerk/clerk?id="+id); //這是設(shè)置掃描二維碼后跳轉(zhuǎn)的頁面

? ? ? ? ? ? paramJson.put("width", 430);

//? ? ? ? ? ? paramJson.put("is_hyaline", true);

//? ? ? ? ? ? paramJson.put("auto_color", true);

? ? ? ? ? ? System.out.println("httpURLConnection"+httpURLConnection);

? ? ? ? ? ? System.out.println("paramJson.toString()"+paramJson.toString());

? ? ? ? ? ? printWriter.write(paramJson.toString());

? ? ? ? ? ? // flush輸出流的緩沖

? ? ? ? ? ? printWriter.flush();

? ? ? ? ? ? //開始獲取數(shù)據(jù)

? ? ? ? ? ? BufferedInputStream bis =new BufferedInputStream(httpURLConnection.getInputStream());

//? ? ? ? ? ? OutputStream os = new FileOutputStream(new File(codeUrl));

? ? ? ? ? ? int len;

//? ? ? ? ? ? byte[] arr = new byte[1024];

//? ? ? ? ? ? while ((len = bis.read(arr)) != -1)

//? ? ? ? ? ? {

//? ? ? ? ? ? ? ? os.write(arr, 0, len);

//? ? ? ? ? ? ? ? os.flush();

//? ? ? ? ? ? }

//? ? ? ? ? ? os.flush();

//? ? ? ? ? ? os.close();

? ? ? ? response.setContentType("image/png");

? ? ? ? response.setHeader("Pragma", "no-cache");

? ? ? ? response.setHeader("Cache-Control", "no-cache");

? ? ? ? response.setDateHeader("Expires", 0);

? ? ? ? OutputStream stream = response.getOutputStream();

? ? ? ? byte[] arr1 =new byte[1024];

? ? ? ? while ((len = bis.read(arr1)) != -1)

{

stream.write(arr1, 0, len);

? ? ? ? ? ? stream.flush();

? ? ? ? }

stream.flush();

? ? ? ? stream.close();

? ? ? ? return twoCodeUrl;

? ? }

}

微信官方提供了三種獲取二維碼的接口句占,可看具體微信官方文檔

接下來寫個controller調(diào)用一下CoreController?

```

@RestController

@RequestMapping("/test2")

public class CoreController {

/**

* 接收二維碼

? ? * @param request

? ? * @return

? ? * @throws IOException

*/

? ? @RequestMapping(value="/twoCode",produces="text/html;charset=utf-8")

@ResponseBody

? ? public void twoCode(HttpServletRequest request, HttpServletResponse response,Integer id)throws Exception {

String accessToken = CreateQrcore.getToken();

? ? ? ? String twoCodeUrl = CreateQrcore.getminiqrQr(accessToken,request,response,id);

//? ? ? ? response.setContentType("image/png");

//? ? ? ? response.setHeader("Pragma", "no-cache");

//? ? ? ? response.setHeader("Cache-Control", "no-cache");

//? ? ? ? response.setDateHeader("Expires", 0);

//? ? ? ? OutputStream stream = response.getOutputStream();

//? ? ? ? byte[] arr = new byte[1024];

//? ? ? ? ? ? stream.write(arr);

//? ? ? ? ? ? stream.flush();

? ? }

}

```

處理post請求的工具類UrlUtil?

public class UrlUtil {

/**

* 向指定 URL 發(fā)送POST方法的請求

*

? ? * @param url 發(fā)送請求的 URL

? ? * @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果

*/

? ? public static StringsendPost(String url, Map paramMap) {

PrintWriter out =null;

? ? ? ? BufferedReader in =null;

? ? ? ? String result ="";

? ? ? ? String param ="";

? ? ? ? Iterator it = paramMap.keySet().iterator();

? ? ? ? while(it.hasNext()) {

String key = it.next();

? ? ? ? ? ? param += key +"=" + paramMap.get(key) +"&";

? ? ? ? }

try {

URL realUrl =new URL(url);

? ? ? ? ? ? // 打開和URL之間的連接

? ? ? ? ? ? URLConnection conn = realUrl.openConnection();

? ? ? ? ? ? // 設(shè)置通用的請求屬性

? ? ? ? ? ? conn.setRequestProperty("accept", "*/*");

? ? ? ? ? ? conn.setRequestProperty("connection", "Keep-Alive");

? ? ? ? ? ? conn.setRequestProperty("Accept-Charset", "utf-8");

? ? ? ? ? ? conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

? ? ? ? ? ? // 發(fā)送POST請求必須設(shè)置如下兩行

? ? ? ? ? ? conn.setDoOutput(true);

? ? ? ? ? ? conn.setDoInput(true);

? ? ? ? ? ? // 獲取URLConnection對象對應(yīng)的輸出流

? ? ? ? ? ? out =new PrintWriter(conn.getOutputStream());

? ? ? ? ? ? // 發(fā)送請求參數(shù)

? ? ? ? ? ? out.print(param);

? ? ? ? ? ? // flush輸出流的緩沖

? ? ? ? ? ? out.flush();

? ? ? ? ? ? // 定義BufferedReader輸入流來讀取URL的響應(yīng)

? ? ? ? ? ? in =new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

? ? ? ? ? ? String line;

? ? ? ? ? ? while ((line = in.readLine()) !=null) {

result += line;

? ? ? ? ? ? }

}catch (Exception e) {

System.out.println(e);

? ? ? ? }

//使用finally塊來關(guān)閉輸出流限佩、輸入流

? ? ? ? finally{

try{

if(out!=null){

out.close();

? ? ? ? ? ? ? ? }

if(in!=null){

in.close();

? ? ? ? ? ? ? ? }

}

catch(Exception ex){

ex.printStackTrace();

? ? ? ? ? ? }

}

return result;

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末衷恭,一起剝皮案震驚了整個濱河市哎壳,隨后出現(xiàn)的幾起案子鸳慈,更是在濱河造成了極大的恐慌拂募,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件帅刊,死亡現(xiàn)場離奇詭異纸泡,居然都是意外死亡,警方通過查閱死者的電腦和手機厚掷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門弟灼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來级解,“玉大人冒黑,你說我怎么就攤上這事∏诨” “怎么了抡爹?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長芒划。 經(jīng)常有香客問我冬竟,道長,這世上最難降的妖魔是什么民逼? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任泵殴,我火速辦了婚禮,結(jié)果婚禮上拼苍,老公的妹妹穿的比我還像新娘笑诅。我一直安慰自己,他們只是感情好疮鲫,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布吆你。 她就那樣靜靜地躺著,像睡著了一般俊犯。 火紅的嫁衣襯著肌膚如雪妇多。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天燕侠,我揣著相機與錄音者祖,去河邊找鬼。 笑死绢彤,一個胖子當(dāng)著我的面吹牛咸包,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播杖虾,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼烂瘫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坟比,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤芦鳍,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后葛账,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柠衅,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年籍琳,在試婚紗的時候發(fā)現(xiàn)自己被綠了菲宴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡趋急,死狀恐怖喝峦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呜达,我是刑警寧澤谣蠢,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站查近,受9級特大地震影響眉踱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜霜威,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一谈喳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧戈泼,春花似錦婿禽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至胎署,卻和暖如春吆录,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背琼牧。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工恢筝, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人巨坊。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓撬槽,卻偏偏與公主長得像,于是被迫代替她去往敵國和親趾撵。 傳聞我的和親對象是個殘疾皇子侄柔,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容