10.小程序工具类
public synchronized static String sendPost(String url, Map<String, ?> paramMap) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String param = "";
Iterator<String> it = paramMap.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
param += key + "=" + paramMap.get(key) + "&";
}
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Accept-Charset", "utf-8");
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(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public synchronized static String sendPostJson(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.info("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
public synchronized static JSONObject getSessionKeyOropenid(String code, String appid, String secret) {
String wxCode = code;
String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
Map<String, String> requestUrlParam = new HashMap<String, String>();
requestUrlParam.put("appid", appid);
requestUrlParam.put("secret", secret);
requestUrlParam.put("js_code", wxCode);
requestUrlParam.put("grant_type", "authorization_code");
JSONObject jsonObject = JSON.parseObject(sendPost(requestUrl, requestUrlParam));
return jsonObject;
}
private static String getAccessToken(String accessTokenURL, Map<String, String> requestParam) {
String access_token;
JSONObject jsonObject = JSON.parseObject(sendPost(accessTokenURL, requestParam));
access_token = jsonObject.getString("access_token");
return access_token;
}
public synchronized static String getSessionKeyOropenid(String appid, String secret) {
String accessTokenURL = "https://api.weixin.qq.com/cgi-bin/token";
Map<String, String> requestParam = new HashMap<String, String>();
requestParam.put("grant_type", "client_credential");
requestParam.put("appid", appid);
requestParam.put("secret", secret);
return getAccessToken(accessTokenURL, requestParam);
}
public synchronized static JSONObject getUserInfo(String encryptedData, String sessionKey, String iv) {
byte[] dataByte = Base64.decode(encryptedData);
byte[] keyByte = Base64.decode(sessionKey);
byte[] ivByte = Base64.decode(iv);
try {
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, "UTF-8");
return JSON.parseObject(result);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return null;
}
public synchronized static Boolean getminiqrQr(String accessToken, String serviceCode, String picPath, String path) {
try {
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
JSONObject paramJson = new JSONObject();
paramJson.put("scene", serviceCode);
paramJson.put("page", path);
paramJson.put("width", 430);
paramJson.put("is_hyaline", false);
paramJson.put("auto_color", false);
System.out.println("httpURLConnection" + httpURLConnection);
System.out.println("paramJson.toString()" + paramJson.toString());
printWriter.write(paramJson.toString());
printWriter.flush();
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File(picPath));
int len;
byte[] arr = new byte[1024];
while ((len = bis.read(arr)) != -1) {
os.write(arr, 0, len);
os.flush();
}
os.close();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
public synchronized static String codeImage(String paramete, String appid, String secret, String page) {
String picPathDir = "/upload/oldPushCode";
File picFoler = new File(picPathDir);
if (!picFoler.exists()) {
picFoler.mkdirs();
}
String picPath = picPathDir + "/" + paramete + ".png";
String accessToken = XcxUtils.getSessionKeyOropenid(appid, secret);
String path = page;
String scene = paramete;
getminiqrQr(accessToken, scene, picPath, path);
String image = "/upload/oldPushCode/" + paramete + ".png";
return image;
}