publicString Wx_Signature() {
String path=ServletActionContext.getServletContext().getRealPath("/wx/");//System.out.println(path);
try{
String tokenJSON=MyFileUtils.readText(path);//String access_token = null;//access_token
String atime = null;//获取时间
String a_expires_in = null;//有效时间(s)
String ticket = null;//jsapi_ticket//String ttime = null;//得到时间//String t_expires_in = null;//有效时间(s)
String result =tokenJSON;if (result == null || result.equals("")) {
tokenJSON=getAccessToken_ticket(path);
}//System.out.println(result);
if (tokenJSON != null && !tokenJSON.equals("")&& tokenJSON.indexOf("access_token") != -1) {try{
JSONObject jsonObject= newJSONObject(tokenJSON);//access_token = jsonObject.getString("access_token");//
//access_token
atime = jsonObject.getString("atime");//开始时间
a_expires_in = jsonObject.getString("a_expires_in");//有效时间
ticket = jsonObject.getString("ticket");//jsapi_ticket//System.out.println(ticket);//ttime = jsonObject.getString("ttime");//开始时间//t_expires_in = jsonObject.getString("t_expires_in");//
//有效时间
String t1 =getCurrentDateStr();
String t2=atime;//System.out.println(atime);//System.out.println(a_expires_in);//System.out.println(TimeInterval.getInterval(t2, t1));
long end_time = Long.parseLong(a_expires_in) - 60;if (TimeInterval.getInterval(t2, t1) >end_time) {
ticket=getAccessToken_ticket(path);
}
}catch(JSONException e) {
msg=e.getMessage();
}
}else{
}//System.out.println(ticket);
String url = getParameter("url");
String noncestr= TUtils.getRandomString(16);
String timestamp= System.currentTimeMillis() + "";
timestamp= timestamp.substring(0, 10);
String data= "jsapi_ticket=" + ticket + "&noncestr=" +noncestr+ "×tamp=" + timestamp + "&url=" +url;
String digest= newSHA1().getDigestOfString(data.getBytes());
String signature= digest.toLowerCase();//signature
result = "{\"noncestr\":\"" + noncestr + "\",\"timestamp\":\""
+ timestamp + "\",\"url\":\"" + url + "\",\"signature\":\""
+ signature + "\" ,\"ticket\":\"" + ticket + "\"}";
msg=result;
}catch(IOException e) {
msg=e.getMessage();
}returnmsg
}