本文主要是介绍java生成微信分享相关配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package org.jeecg.modules.caayouth.api.utils.weixinShare;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class WeinXinUtil {
private static String AppId="*************";
private static String Secret="*************";
public static Map<String, String> sign() {
Map<String,String> m=getJsapiTicket();
String jsapi_ticket =m.get("jsapiTicket");
String url = "***********";//分享页面域名
Map<String, String> ret = new HashMap<String, String>();
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String string1;
String signature = "";
//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);
ret.put("accessToken",m.get("accessToken"));
return ret;
}
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
private static String create_nonce_str() {
return UUID.randomUUID().toString();
}
private static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
/*获取jsapi_ticket*/
public static Map<String,String> getJsapiTicket() {
Map<String,String> m=new HashMap<>();
String accessToken=getAccessToken("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" +
"&appid=AppId&secret=Secret");
JSONObject demoJson = JSONObject.fromObject(accessToken);
m.put("accessToken",demoJson.getString("access_token"));
String jsapiTicketURL="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="+m.get("accessToken");
String result = "";
BufferedReader in = null;
try {
String urlNameString = jsapiTicketURL ;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("异常" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
JSONObject jsapiTicket = JSONObject.fromObject(result);
m.put("jsapiTicket",jsapiTicket.getString("ticket"));
return m;
}
/*获取accessToken*/
public static String getAccessToken(String url) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url ;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("异常" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
这篇关于java生成微信分享相关配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!