多借鉴网上大神的代码和微信开发文档
获取token根据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"; //二维码生产的地址 本地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);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter =new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson =new JSONObject();
// paramJson.put("scene","2");//这就是你二维码里携带的参数 String型 名称不可变
paramJson.put("path", "pages/order/clerk/clerk?id="+id); //这是设置扫描二维码后跳转的页面
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();
//开始获取数据
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调用一下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 发送POST方法的请求
*
* @param url 发送请求的 URL
* @return 所代表远程资源的响应结果
*/
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();
// 设置通用的请求属性
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)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out =new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
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块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
return result;
}
}