多借鑒網(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;
? ? }
}