一、创建工具类HttpRequestUtil
public class HttpRequestUtil {
public static String doGet(String url, Map<String, String> param) {
RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(60000).build();
CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
String resultString = "";
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpclient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String getWxAccessToken(String APPID, String SECRET) throws JSONException {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + SECRET;
String json = HttpRequestUtil.doGet(url);
JSONObject obj = new JSONObject(json);
return (String) obj.get("access_token");
}
public static byte[] getminiqrQr(String url, Map<String, Object> paraMap) throws Exception {
byte[] result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
JSONObject postData = new JSONObject();
for (Map.Entry<String, Object> entry : paraMap.entrySet()) {
postData.put(entry.getKey(), entry.getValue());
}
httpPost.setEntity(new StringEntity(postData.toString(), "UTF-8"));
HttpClient httpClient = HttpClientBuilder.create().build();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toByteArray(entity);
return result;
}
public static void downloadQrCode(HttpServletResponse response, String wxurl, int width, String APPID, String SECRET) throws Exception {
String accessToken =getWxAccessToken(APPID, SECRET);
byte[] qrCodeBytes = null;
Map<String, Object> paraMap = new HashMap();
String url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
url += accessToken;
paraMap.put("path", wxurl);
paraMap.put("width",width);
qrCodeBytes=getminiqrQr(url,paraMap);
response.setContentType("image/jpg");
OutputStream stream = response.getOutputStream();
stream.write(qrCodeBytes);
stream.flush();
stream.close();
}
}
二、创建Controller提供生成二维码接口
@Controller
@RequestMapping(value = "/share")
public class JobManagementController {
public static final String APPID="wxac123456789154";
public static final String SECRET="14521545dffjsdd5f45d4fds5f4sd1";
@GetMapping(value = "/code")
public void policyCode(String uuid, HttpServletResponse response, HttpServletRequest request) {
try {
String wxurl = "/wx/job/getJobInfo/" + uuid;
HttpRequestUtil.downloadQrCode(response,wxurl, 280, APPID, SECRET);
}catch (Exception e){
e.printStackTrace();
}
}
}