@Autowired
private RestTemplate restTemplate;
public void getQrCode(HttpServletResponse response, code) {
// req举例{“scene”:“500003#461”,“page”:“pages/index/index”,“width”:300}
JSONObject object = new JSONObject();
object.put("scene", "code=" + code);
byte[] bytes = restTemplate.postForObject(url, object, byte[].class);
String fileName = DateUtil.now() + "-小程序码";
try {
response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1") + ".jpg");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(bytes);
response.flushBuffer();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
微信开发 Java SDK:https://gitee.com/binary/weixin-java-tools