微信分享生成自己的二维码
1.获取自己的appid和Secret
其中的appid和secret是项目的
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;
}
2.根据以上生成的access token来生成二维码
public static String getQRCode(HttpServletResponse response,Object scene,String page)throws Exception {
String property = System.getProperty("user.dir");
String ctxPath = property;
String fileName="twoCode.png";
String bizPath = "img";
String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
String ppath =ctxPath + File.separator + bizPath + File.separator + nowday;
File file1 = new File(ctxPath + File.separator + bizPath + File.separator + nowday);
if (!file1.exists()) {
file1.mkdirs();// 创建文件根目录
}
String savePath = file1.getPath() + File.separator + fileName;
String qrCode = bizPath + File.separator + nowday+ File.separator + fileName;
// if (ppath.contains("\\")) {
// ppath = ppath.replace("\\", "/");
// }
if (qrCode.contains("\\")) {
qrCode = qrCode.replace("\\", "/");
}
/**
在此输入自己的appid和Secret
**/
String wxspAppid = "appid" ;
//小程序的 app secret (在微信小程序管理后台获取)
String wxspSecret = "Secret ";
String accessToken = WxQrCode.getAccessToken(wxspAppid,wxspSecret);
String wxCodeURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
URL url = new URL(wxCodeURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", scene);
paramJson.put("page", page); //小程序暂未发布我没有带page参数
System.out.println(paramJson);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File(savePath));
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1)
{
os.write(arr, 0, len);
os.flush();
}
os.close();
//实现了生成二维码并保存在本地电脑的硬盘中。
File file = new File(savePath);
FileInputStream fis = new FileInputStream(file);
long size = file.length();
byte[] temp = new byte[(int) size];
fis.read(temp, 0, (int) size);
fis.close();
byte[] data = temp;
response.setContentType("image/png");
OutputStream out = response.getOutputStream();
out.write(data);
out.flush();
out.close();
}
3.总结
String property = System.getProperty("user.dir");
1.以上是根据项目来获取根路径
2.二维码生成失败:
a:要不是你page路径小程序未上线或者是你传入的scene的参数不正确
item_id=118149&shop_id=249
这是正确的,scene最大32个可见字符长度有限,如果你传入的参数较多需要单独进行处理
b:读取的时候会报image = null 明明所在的文件有照片就是读取失败,这个问题可能是你读取照片的时候格式不同造成的,网上有好多解决办法
3.二维码生成了但是扫描后显示参数丢失,那就需要看看前端接收的时候是以什么格式接收的,以什么参数接收的,这就是scene的参数问题;