1、调用微信接口生成二维码工具类
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Objects;
public class WxCodeUtil {
public String postToken(String appId, String appsecret) throws Exception {
String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appsecret;
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
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 in;
if (requestUrl.contains("nlp"))
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
else
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuilder result = new StringBuilder();
String getLine;
while ((getLine = in.readLine()) != null) {
result.append(getLine);
}
in.close();
JSONObject jsonObject = JSONObject.parseObject(result.toString());
return jsonObject.getString("access_token");
}
public Object getQrCodeImgUrl(String scene,int width,String page,String token) {
String createQrCodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token;
JSONObject createQrParam = new JSONObject();
createQrParam.put("scene", scene);
createQrParam.put("page", page);
createQrParam.put("width", width);
createQrParam.put("env_version","trial");
PrintWriter out = null;
InputStream in = null;
try {
URL realUrl = new URL(createQrCodeUrl);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(createQrParam);
out.flush();
in = conn.getInputStream();
byte[] data = null;
try {
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = in.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
data = swapStream.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String base64Code = new String(Objects.requireNonNull(Base64.encodeBase64(data)));
System.out.println("方法输出开始--");
System.out.println(base64Code);
System.out.println("方法输出结束--");
return base64Code;
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return "";
}
public BufferedImage convertBase64ToImage(String base64Code){
BufferedImage image = null;
byte[] imageByte = null;
try {
imageByte = DatatypeConverter.parseBase64Binary(base64Code);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(new ByteArrayInputStream(imageByte));
bis.close();
return imgage;
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、设置接收微信二维码
public class Text{
@Resource
private WxCodeUtil wxcode;
private static final String appid="";
private static final String appsercet="";
public void exportCode(HttpServletResponse response){
String token = wxcode.postToken(appid,appsercet);
int width = 300;
String baseCode = wxcode.getQrCodeImgUrl("扫描二维码携带的参数",width,"小程序页面",token).toString();
BufferedImage code = wxcode.convertBase64ToImage(baseCode);
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=wxcode.jpg");
OutputStream outputStream = response.getOutputStream();
ImageIO.write(code, "jpg", outputStream);
outputStream.flush();
outputStream.close();
}
public String wxCodeBase64(HttpServletResponse response){
String token = wxcode.postToken(appid,appsercet);
int width = 300;
String baseCode = wxcode.getQrCodeImgUrl("扫描二维码携带的参数",width,"小程序页面",token).toString();
return baseCode;
}
}
3、前端拿到base64显示在页面中
<img src="data:image/png;base64,后端返回的base64" />