package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.common.collect.Maps;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.mock.web.MockMultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import java.io.*;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.nio.Buffer;
|
import java.nio.charset.Charset;
|
import java.util.Map;
|
|
public class QrCodeUtil {
|
public static void main(String[] args) {
|
System.out.print(getQrCode(975));
|
}
|
public static String getQrCode(Integer uid){
|
|
String appid="wx94a1a55229c933ac";//应用ID
|
String appSecret="cbd89baf017146c1d56c00f1d2a59df6";//(应用密钥)
|
String urlToken ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";
|
String backData=sendGet(urlToken, "utf-8", 10000);
|
String accessToken = JSONObject.parseObject(backData).getString("access_token");
|
String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+accessToken;
|
|
Map<String,Object> map = Maps.newHashMap();
|
map.put("scene", "driverId="+uid);
|
map.put("page", "pages/home/scanPage/scanPage");
|
String jsonString = JSON.toJSONString(map);
|
|
byte[] data = post(url, jsonString);
|
MultipartFile file = getMultipartFile(data);
|
String ossUpload="";
|
try {
|
ossUpload = OssUploadUtil.ossUpload( file);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return ossUpload;
|
}
|
//二进制文件转换MultipartFile
|
private static MultipartFile getMultipartFile(byte[] bytes) {
|
System.out.println("二进制转换MultipartFile开始");
|
MultipartFile mockMultipartFile = null;
|
//java7新特性 不用手动关闭流
|
try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
|
mockMultipartFile = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
|
} catch (IOException e) {
|
e.printStackTrace();
|
System.out.println("二进制文件转换图片异常");
|
}
|
return mockMultipartFile;
|
}
|
|
/* 发送 post请求 用HTTPclient 发送请求*/
|
public static 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");
|
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();
|
}
|
/***
|
* 模拟get请求
|
* @param url
|
* @param charset
|
* @param timeout
|
* @return
|
*/
|
public static String sendGet(String url, String charset, int timeout)
|
{
|
String result = "";
|
try
|
{
|
URL u = new URL(url);
|
try
|
{
|
URLConnection conn = u.openConnection();
|
conn.connect();
|
conn.setConnectTimeout(timeout);
|
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
|
String line="";
|
while ((line = in.readLine()) != null)
|
{
|
|
result = result + line;
|
}
|
in.close();
|
} catch (IOException e) {
|
return result;
|
}
|
}
|
catch (MalformedURLException e)
|
{
|
return result;
|
}
|
|
return result;
|
}
|
}
|