• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信生成小程序二维码

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
1、生成二维码的工具类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

@Slf4j
public class WxQrCode {

//获取AccessToken路径
private static final String AccessToken_URL
= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";//小程序id
//获取二维码路径
private static final String WxCode_URL
= "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN";//小程序密钥
/**
* 用于获取access_token
* @return access_token
* @throws Exception
*/
public static String getAccessToken(String appid,String secret) throws Exception {
String requestUrl = AccessToken_URL.replace("APPID",appid).replace("APPSECRET",secret);
URL url = new URL(requestUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
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"));
else
in = 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;
}


/*
* 获取 二维码图片
*
*/
public static InputStream getminiqrQr(String accessToken,String scene, String page) {
InputStream is = null;
try
{
String wxCodeURL = WxCode_URL.replace("ACCESS_TOKEN",accessToken);
URL url = new URL(wxCodeURL);
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();
if(!StringUtils.isEmpty(scene)){
paramJson.put("scene", scene);
}
if(!StringUtils.isEmpty(page)) {
paramJson.put("page", page);
}
// paramJson.put("page", "pages/index/index"); //小程序暂未发布我没有带page参数
paramJson.put("width", 430);
paramJson.put("is_hyaline", true);
paramJson.put("auto_color", true);

printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
is = httpURLConnection.getInputStream();
}catch (Exception e){
e.printStackTrace();
}
return is;
}

public static void main(String[] args) {
String APIKEY = "wxbc6e00e3f4ba7128";
String SECRETKEY = "4db0fdadb56a95fb6d29ba78b5d9dd21";
String accessToken = null;
try{
accessToken = WxQrCode.getAccessToken(APIKEY,SECRETKEY);
System.out.println("accessToken;"+accessToken);
InputStream is = WxQrCode.getminiqrQr(accessToken,"1","pages/common/vip/vipJoin");

String filepath = "C:\\Users\\10320\\Desktop\\c.jpg";
FileOutputStream os = new FileOutputStream(filepath);

byte[] bytes = new byte[1024];
int length;
while((length = is.read(bytes))>0){
os.write(bytes,0,length);
}
os.close();
is.close();
}catch (Exception e){
e.printStackTrace();
}
}

}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap