/**
* @param 用于获取access_token
* @param apiKey 小程序id
* @param secretKey 小程序密钥
* @return access_token
* @throws Exception
*/
public String postToken() throws Exception {
String apiKey = Conf.getWxAppid();//小程序id
String secretKey = Conf.getWxAppScrect();//小程序密钥
String grant_type = "client_credential";
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+apiKey+"&secret="+secretKey;
URL url = new URL(requestUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置通用的请求属性
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;
}
/**
* 生成带参小程序二维码
* @param scene 要输入的内容
* @param accessToken token
*/
public String postMiniqrQr(String scene) {
String encode = null;
try{
String accessToken = this.postToken();
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true); // 打开写入属性
httpURLConnection.setDoInput(true); // 打开读取属性
httpURLConnection.setRequestMethod("POST");// 提交方式
// 不得不说一下这个提交方式转换!!真的坑。。改了好长时间!!一定要记得加响应头
httpURLConnection.setRequestProperty("Content-Type", "application/x-javascript; charset=UTF-8");// 设置响应头
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", scene); // 你要放的内容
paramJson.put("path", Conf.getWxAppPath());
paramJson.put("width", 430); // 宽度
paramJson.put("auto_color", true);
paramJson.put("is_hyaline", true);
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
if (bis != null) {
byte[] buffer = new byte[1024];
//每次读取的字符串长度,如果为-1,代表全部读取完毕
int len;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while ((len = bis.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
bis.close();
byte[] bytes = outStream.toByteArray();
// wxQrCode.read(bytes);
BASE64Encoder base64Encoder = new BASE64Encoder();
encode = base64Encoder.encode(bytes);
}
return encode;
// //创建一个空文件
// OutputStream os = new FileOutputStream(new File("E:/test/test3.jpg"));
// //ByteArrayOutputStream os = new ByteArrayOutputStream();
// int len;
// byte[] arr = new byte[1024];
// while ((len = bis.read(arr)) != -1)
// {
// os.write(arr, 0, len);
// os.flush();
// }
// os.close();
// 上传云储存
//InputStream is = new ByteArrayInputStream(os.toByteArray());
//retMap = UploadUtils.upload(is);
}
catch (Exception e)
{
e.printStackTrace();
}
return encode;
}
在官方文档写的很清楚scene只能是String类型,所以和前端要商量好传输格式