官方地址:
https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
直接上代码:
获取工具类,QrCodeTalkUtils.java
package com.dlys.wechat;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import com.dlyspublic.util.Config;
import com.dlyspublic.util.Const;
import com.dlyspublic.util.QrCodeEnum;
import com.dlyspublic.wxpub.WxAccessTokenBo;
/**
* 获取对讲二维码
*
* @author libaibai
* @version 1.0 2020年10月29日
*/
@Component
public class QrCodeTalkUtils {
private static final Logger LOG = LogManager.getLogger(QrCodeTalkUtils.class);
private static final String URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit";// 获取二维码
@Resource
HttpPost httpPost;
@Resource
WxAccessTokenBo wxAccessTokenBo;
/**
* 获取停车场二维码
*
* @param punitId
* @param cid
* @return
*/
public byte[] getParkCode(long punitId, long cid) {
return send(punitId, QrCodeEnum.PARK.getValue() + "-" + cid);
}
/**
* 获取门禁二维码
*
* @param punitId
* @param cid
* @return
*/
public byte[] getDoorCode(long punitId, long cid) {
return send(punitId, QrCodeEnum.DOOR.getValue() + "-" + cid);
}
/**
* 获取二维码
*
* @param punitId
* @param cid
*/
private byte[] send(long punitId, String cid) {
Map<String, Object> requestMap = new HashMap<String, Object>();
requestMap.put("scene", cid);// "C-cid" 通道唯一标识
requestMap.put("page", "pages/main/main");
// 发起请求
byte[] response = null;
String access_token = null;
Map<String, String> map = wxAccessTokenBo.getLocalAccessToken(Config.TALKAPPID, Config.TALKSECRET);
if (map != null) {
access_token = Const.getStr(map.get("accessToken"));
}
if (StringUtils.isEmpty(access_token)) {
return null;
}
try {
response = httpPost.sendJsonObject(URL + "?access_token=" + access_token, requestMap);
} catch (Exception e) {
LOG.error("获取对讲二维码失败", e);
}
LOG.info("获取对讲二维码:" + response);
return response;
}
}
HttpPost.java
package com.dlys.wechat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Resource;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import com.dlyspublic.util.FileUploadUtil;
import com.google.gson.Gson;
/**
*
* @author libaibai
* @version 1.0 2020年10月4日
*/
@Component
public class HttpPost {
@Resource
FileUploadUtil fileupload;
@Resource(name = "terminalupload")
private Properties terminalupload; // 直接引用terminalupload.properties文件,直接用
/**
* @param httpUrl
* @param params
* @param headerParams
* @return
*/
public static String send(final String httpUrl, final Map<String, String> params,
final Map<String, String> headerParams) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
// params
if (params != null && !params.isEmpty()) {
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : params.entrySet()) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
requestBuilder.setEntity(new UrlEncodedFormEntity(formParams, Consts.UTF_8));
}
// headers
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param params
* @return
*/
public static String send(final String httpUrl, final Map<String, String> params) throws Exception {
return send(httpUrl, params, null);
}
/**
* @param httpUrl
* @return
*/
public static String send(final String httpUrl) throws Exception {
return send(httpUrl, null, null);
}
/**
* @param httpUrl
* @param params
* @param headerParams
* @return
*/
public static String sendJson(final String httpUrl, final Map<String, String> params,
final Map<String, String> headerParams) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
// params
if (params != null && !params.isEmpty()) {
requestBuilder.setEntity(new StringEntity(new Gson().toJson(params), ContentType.APPLICATION_JSON));
}
// headers
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param params
* @return
*/
public static String sendJson(final String httpUrl, final Map<String, String> params) throws Exception {
return sendJson(httpUrl, params, null);
}
/**
* @param httpUrl
* @return
*/
public static String sendJson(final String httpUrl) throws Exception {
return sendJson(httpUrl, null, null);
}
/**
* @param httpUrl
* @param xmlStr
* @param headerParams
* @return
*/
public static String sendXml(final String httpUrl, final String xmlStr, final Map<String, String> headerParams)
throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig())
// .setEntity(new StringEntity(xmlStr,
// ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8)));
.setEntity(new StringEntity(xmlStr, ContentType.create("application/xml", Consts.UTF_8)));
// headers
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param xmlStr
* @return
*/
public static String sendXml(final String httpUrl, final String xmlStr) throws Exception {
return sendXml(httpUrl, xmlStr, null);
}
/**
* @param httpUrl
* @return
*/
public static String sendXml(final String httpUrl) throws Exception {
return sendXml(httpUrl, null, null);
}
/**
* @param httpUrl
* @param params
* @return
*/
public String sendJsonObject2(final String httpUrl, final Map<String, Object> params,Long punitId,String imgUrl) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
FileOutputStream fout = null;
InputStream in = null;
String imgPath = String.valueOf(terminalupload.get("imgPath"));
String wxa = String.valueOf(terminalupload.get("wxa"));
String imgUrlAssembly = imgPath+wxa+punitId+"/";
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
// params
if (params != null && !params.isEmpty()) {
requestBuilder.setEntity(new StringEntity(GsonUtil.toJson(params), ContentType.APPLICATION_JSON));
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
HttpEntity httpEntity=response.getEntity(); //4、获取实体
in = httpEntity.getContent();
File file = new File(imgUrlAssembly);
if(!file.exists()) {
file.mkdirs();
}
fout = new FileOutputStream(imgPath+wxa+imgUrl);
int l = -1;
byte[] tmp = new byte[1024];
while ((l = in.read(tmp)) != -1) {
fout.write(tmp, 0, l);
}
fout.flush();
fout.close();
in.close();
// String imgName=imgUrl.substring(imgUrl.lastIndexOf("/")+1);
// FileUploadState fileState= fileupload.saveFileStream("wxa", in, punitId, false, null, imgName, null, "jpg", null, 0, 0);
return "SUCCEES";
} finally {
// 关闭低层流。
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param object
* @return
*/
public byte[] sendJsonObject(final String httpUrl, final Map<String, Object> params) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
// params
if (params != null) {
requestBuilder.setEntity(new StringEntity(GsonUtil.toJson(params), ContentType.APPLICATION_JSON));
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
byte[] img = EntityUtils.toByteArray(response.getEntity());
return img;
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* 携带图片 multipartEntityBuilder.addPart(entry.getKey(), new
* StringBody(entry.getValue(), ContentType.TEXT_PLAIN));
* multipartEntityBuilder.addPart("comment", comment);
*
* @param httpUrl
* @param params
* @param fileParams
* @return
*/
public static String sendMultipart(final String httpUrl, final Map<String, String> params,
final Map<String, File> fileParams) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// params
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntityBuilder.addPart(entry.getKey(),
new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
}
}
// inputStreams
if (fileParams != null) {
for (Map.Entry<String, File> entry : fileParams.entrySet()) {
if (entry.getValue() != null) {
multipartEntityBuilder.addPart(entry.getKey(), new FileBody(entry.getValue()));
}
}
}
requestBuilder.setEntity(multipartEntityBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* 携带图片 multipartEntityBuilder.addPart(entry.getKey(), new
* StringBody(entry.getValue(), ContentType.TEXT_PLAIN));
* multipartEntityBuilder.addPart("comment", comment);
*
* @param httpUrl
* @param headerParams
* @param fileParams
* @return
*/
public static String sendMultipart1(final String httpUrl, final Map<String, String> headerParams,
final Map<String, File> fileParams) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// headers
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
// inputStreams
if (fileParams != null) {
for (Map.Entry<String, File> entry : fileParams.entrySet()) {
if (entry.getValue() != null) {
multipartEntityBuilder.addPart(entry.getKey(), new FileBody(entry.getValue()));
}
}
}
requestBuilder.setEntity(multipartEntityBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param params
* 普通参数
* @param inputStreams
* 数据流参数
* @return
*/
public static String sendMultipartByInputStreamBody(final String httpUrl, final Map<String, String> params,
final Map<String, InputStreamBody> inputStreams) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// params
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntityBuilder.addPart(entry.getKey(),
new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
}
}
// inputStreams
if (inputStreams != null && !inputStreams.isEmpty()) {
for (Map.Entry<String, InputStreamBody> entry : inputStreams.entrySet()) {
String fileName = entry.getKey();
InputStreamBody inputStreamBody = entry.getValue();
multipartEntityBuilder.addPart(fileName, inputStreamBody);
}
}
requestBuilder.setEntity(multipartEntityBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @param inputStreams
* 数据流参数
* @return
*/
public static String sendMultipartByInputStreamBody(final String httpUrl,
final Map<String, InputStreamBody> inputStreams) throws Exception {
return sendMultipartByInputStreamBody(httpUrl, null, inputStreams);
}
/**
* 上传图片
*
* @param httpUrl
* @param params
* @param byteArrays
* @return
*/
public static String sendMultipartByByteArrayBody(final String httpUrl, final Map<String, String> params,
final Map<String, ByteArrayBody> byteArrays) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// params
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
multipartEntityBuilder.addPart(entry.getKey(),
new StringBody(entry.getValue(), ContentType.create("text/plain", Consts.UTF_8)));
}
}
// byteArrays
if (byteArrays != null && !byteArrays.isEmpty()) {
for (Map.Entry<String, ByteArrayBody> entry : byteArrays.entrySet()) {
String fileName = entry.getKey();
ByteArrayBody byteArrayBody = entry.getValue();
multipartEntityBuilder.addPart(fileName, byteArrayBody);
}
}
requestBuilder.setEntity(multipartEntityBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* 上传图片
*
* @param httpUrl
* @param headerParams
* @param byteArrays
* @return
*/
public static String sendMultipartByByteArrayBody1(final String httpUrl, final Map<String, String> headerParams,
final Map<String, ByteArrayBody> byteArrays) throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
// headers
if (headerParams != null) {
for (Map.Entry<String, String> entry : headerParams.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
}
// byteArrays
if (byteArrays != null && !byteArrays.isEmpty()) {
for (Map.Entry<String, ByteArrayBody> entry : byteArrays.entrySet()) {
String fileName = entry.getKey();
ByteArrayBody byteArrayBody = entry.getValue();
multipartEntityBuilder.addPart(fileName, byteArrayBody);
}
}
requestBuilder.setEntity(multipartEntityBuilder.build());
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* 上传图片
*
* @param httpUrl
* @param byteArrays
* @return
*/
public static String sendMultipartByByteArrayBody(final String httpUrl, final Map<String, ByteArrayBody> byteArrays)
throws Exception {
return sendMultipartByByteArrayBody(httpUrl, null, byteArrays);
}
/**
* @param httpUrl
* @param params
* @return
*/
public static byte[] sendAndReceiveByteArray(final String httpUrl, final Map<String, String> params)
throws Exception {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try {
httpClient = HttpClients.createDefault();
RequestBuilder requestBuilder = RequestBuilder.post().setUri(new URI(httpUrl))
.setConfig(Config.getRequestConfig());
// params
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
requestBuilder.addParameter(entry.getKey(), entry.getValue());
}
}
HttpUriRequest httpUriRequest = requestBuilder.build();
response = httpClient.execute(httpUriRequest);
return EntityUtils.toByteArray(response.getEntity());
} finally {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
}
}
/**
* @param httpUrl
* @return
*/
public static byte[] sendAndReceiveByteArray(final String httpUrl) throws Exception {
return sendAndReceiveByteArray(httpUrl, null);
}
// public static void main(String[] args) throws Exception {
// // 下载图片
// Map<String, String> params = new HashMap<String, String>();
// params.put("file", "00171221102849211001211.jpg");
// byte[] bytes =
// HttpGet.sendAndReceiveByteArray("http://192.168.200.151:2080/uc_file-srv/download.do",
// params);
// // 上传图片
// if (bytes != null && bytes.length > 0) {
// Map<String, ByteArrayBody> params2 = new HashMap<String, ByteArrayBody>();
// params2.put("file", new ByteArrayBody(bytes, "00171221102849211001212.jpg"));
// String json =
// HttpPost.sendMultipartByByteArrayBody("http://192.168.200.151:2080/uc_file-srv/upload.do",
// null, params2);
// System.out.println(json);
// }
// }
}
GsonUtil.java
package com.dlys.wechat;
import java.lang.reflect.Type;
import com.google.gson.Gson;
/**
*
* @author libaibai
* @version 1.0 2020年10月4日
*/
public class GsonUtil {
private static Gson GSON = new Gson();
public static <T> T fromJson(String json, Type type) {
return GSON.fromJson(json, type);
}
public static <T> T fromJson(String json, Class<T> classOfT) {
return GSON.fromJson(json, classOfT);
}
public static <T> T fromJsonp(String jsonp, Class<T> classOfT) {
String json = jsonp.substring(jsonp.indexOf("{"), jsonp.lastIndexOf("}") + 1);
return GSON.fromJson(json, classOfT);
}
public static String toJson(Object src) {
return GSON.toJson(src);
}
}
Config.java
package com.dlys.wechat;
import java.io.File;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
public class Config {
public static RequestConfig getRequestConfig() {
return RequestConfig.custom()
.setConnectTimeout(12000) // 设置连接超时时间,单位毫秒。
.setConnectionRequestTimeout(12000) // 设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
.setSocketTimeout(12000) // 请求获取数据的超时时间,单位毫秒。如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
.build();
}
public static CloseableHttpClient getHttpClients(final File sslJks, final String sslPwd) throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException {
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(sslJks, sslPwd.toCharArray(),
new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(
sslcontext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
return HttpClients.custom()
.setSSLSocketFactory(sslSf)
.build();
}
/**
* 忽略证书验证请求
*/
public static CloseableHttpClient createSSLClientDefault() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
//信任所有证书
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslFactory).build();
}
}
下面就是调用类,这里是直接把字节数组返回到前端显示,不在本地保存。
byte[] qrcodeData = null;
if (bindType == 1) {
qrcodeData = qrCodeTalkUtils.getParkCode(punitId, cid);
} else if (bindType == 2) {
qrcodeData = qrCodeTalkUtils.getDoorCode(punitId, cid);
}
String qrcode = Base64.encodeBase64String(qrcodeData);
returnMap.put("qrcode", qrcode);
前台接口action代码
/**
* 获取二维码
*
* @return
*/
public String generateQrCodeTalk() {
Integer tenantId = SessionUtil.getTenantId(); // 租户ID
Map<String, Object> returnMap = this.toMap(punitService.generateQrCodeTalk(tenantId, punitId, bindType, cid));
String qrcode = Const.getStr(returnMap.get("qrcode"));
if (StringUtils.isEmpty(qrcode)) {
return ERROR;
}
// 输出图片到页面
/**
* <pre>
* 1. InputStream is = null;不能使用局部变量,必须使用全局变量,且保证不能为null,不然会报错:
* Check the <param name="inputName"> tag specified for this action
*
* 2.使用PrintWriter out = response.getWriter();会报错 getWriter() has already been called for this response;
* 所以这里使用response.getOutputStream();
* </pre>
*/
ServletOutputStream out = null;
try {
HttpServletResponse response = ServletActionContext.getResponse();
response.reset();
out = response.getOutputStream();
// PrintWriter out = response.getWriter()
inputStream = new ByteArrayInputStream(Base64.decodeBase64(qrcode));
int a = inputStream.read();
while (a != -1) {
out.print((char) a);
try {
a = inputStream.read();
} catch (Exception e) {
}
}
} catch (Exception e) {
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
return SUCCESS;
}
最后显示在jsp页面。
<img id="qrcodetalk" src="/punitWS/generateQrCodeTalk?punitId=<%=punitId%>&cid=<%=id %>&bindType=2">