restTemplate方式
@RequestMapping("external/getQrCode")
public void getAppQrCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
// 设置代理,因为我们公司访问外网需要代理,所以设置代理,如果没有这个要求,就不用设置
requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("XX.XX.XX.XX", 8080)));
restTemplate.setRequestFactory(requestFactory);
// 获取token
String token = tokenController.getSPAccessToken();
// 配置参数
Map<String, Object> param = new HashMap<>(5);
param.put("scene", "name=asd&&age=18");
param.put("page", "pages/index/index");
param.put("width", 430);
param.put("auto_color", false);
Map<String, Object> lineColor = new HashMap<>(5);
lineColor.put("r", 0);
lineColor.put("g", 120);
lineColor.put("b", 120);
param.put("line_color", lineColor);
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token;
ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, JSON.toJSONString(param), byte[].class);
byte[] result = responseEntity.getBody();
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("qrCode.jpg", "UTF-8"));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
bos.write(result);
bos.close();
}
HttpClient方式
String Path = "F:\\qpa\\" + "qrCode.jpg";
String token = tokenController.getSPAccessToken();
Map<String, Object> param = new HashMap<>(5);
param.put("scene", "name=asd");
// param.put("page", "pages/index/index");
param.put("width", 430);
param.put("auto_color", false);
Map<String, Object> lineColor = new HashMap<>(5);
lineColor.put("r", 0);
lineColor.put("g", 0);
lineColor.put("b", 0);
param.put("line_color", lineColor);
System.out.println(JSON.toJSONString(param));
String json = "{\"width\":430,\"scene\":\"name=asd\"}";
byte[] data = post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token,JSON.toJSONString(param));
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("qrCode.jpg", "UTF-8"));
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
bos.write(data);
//new一个文件对象用来保存图片
File imageFile = new File(Path);
//创建输出流
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//关闭输出流
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte[] post(String uRL, String json) {
String obj = null;
InputStream inputStream = null;
Buffer reader = null;
byte[] data = null;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(uRL);
httppost.addHeader("Content-type", "application/json; charset=utf-8");
httppost.setHeader("Accept", "application/json");
HttpHost host2 = new HttpHost("XX.XX.XX.XX", 8080, "http");
RequestConfig config = RequestConfig.custom().setProxy(host2).build();
httppost.setConfig(config);
try {
StringEntity s = new StringEntity(json, Charset.forName("UTF-8"));
s.setContentEncoding("UTF-8");
httppost.setEntity(s);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
// 获取相应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
inputStream = entity.getContent();
data = readInputStream(inputStream);
}
return data;
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
/**
* 将流 保存为数据数组
*
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 创建一个Buffer字符串
byte[] buffer = new byte[1024];
// 每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
// 使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
// 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 关闭输入流
inStream.close();
// 把outStream里的数据写入内存
return outStream.toByteArray();
}
scene用来设置参数
page用来设置扫码后跳转的页面
其他配置 看官方文档 wxacode.getUnlimited
需要注意的是上面的配置生效的前提是,小程序已经发布,
如果小程序没有发布,那么page字段不会生效并且生成的图片是不合法的,因为上面没有二维码
因此 上面能正常运行的前提是,小程序已经发布才行。