Commit 9b997e71f9aef4fd602637acf870bfa63e92ec55
1 parent
b4bcaaf9
Exists in
master
微信分享
Showing
2 changed files
with
198 additions
and
24 deletions
Show diff stats
src/main/java/com/sincere/student/controller/CommonController.java
@@ -9,6 +9,7 @@ import com.sincere.student.model.Area; | @@ -9,6 +9,7 @@ import com.sincere.student.model.Area; | ||
9 | import com.sincere.student.model.Token; | 9 | import com.sincere.student.model.Token; |
10 | import com.sincere.student.service.CommonService; | 10 | import com.sincere.student.service.CommonService; |
11 | import com.sincere.student.service.ParameterService; | 11 | import com.sincere.student.service.ParameterService; |
12 | +import com.sincere.student.utils.HttpClientUtils; | ||
12 | import io.swagger.annotations.ApiOperation; | 13 | import io.swagger.annotations.ApiOperation; |
13 | import org.springframework.beans.factory.annotation.Autowired; | 14 | import org.springframework.beans.factory.annotation.Autowired; |
14 | import org.springframework.web.bind.annotation.RequestMapping; | 15 | import org.springframework.web.bind.annotation.RequestMapping; |
@@ -57,31 +58,9 @@ public class CommonController { | @@ -57,31 +58,9 @@ public class CommonController { | ||
57 | WxSign wxSign1=new WxSign(); | 58 | WxSign wxSign1=new WxSign(); |
58 | wxSign1.setTimestamp(Long.toString(System.currentTimeMillis())); | 59 | wxSign1.setTimestamp(Long.toString(System.currentTimeMillis())); |
59 | wxSign1.setNoncestr(UUID.randomUUID().toString()); | 60 | wxSign1.setNoncestr(UUID.randomUUID().toString()); |
60 | - AccessToken accessToken=parameterService.getAccessToken(); | ||
61 | - Token token=new Token(); | ||
62 | - if(accessToken!=null) { | ||
63 | - Calendar dateOne=Calendar.getInstance(); | ||
64 | - Calendar dateTwo=Calendar.getInstance(); | ||
65 | - dateOne.setTime(new Date()); | ||
66 | - dateTwo.setTime(accessToken.getCreateTime()); | ||
67 | - long timeOne=dateOne.getTimeInMillis(); | ||
68 | - long timeTwo=dateTwo.getTimeInMillis(); | ||
69 | - long minute=(timeOne-timeTwo)/(1000*60); | 61 | + String token = getToken(); |
70 | 62 | ||
71 | - if(minute<60) token.setAccess_token(accessToken.getAccessToken()); | ||
72 | - else{ | ||
73 | - token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); | ||
74 | - accessToken.setAccessToken(token.getAccess_token()); | ||
75 | - parameterService.insertAccessToken(accessToken); | ||
76 | - } | ||
77 | - } | ||
78 | - else { | ||
79 | - token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); | ||
80 | - accessToken=new AccessToken(); | ||
81 | - accessToken.setAccessToken(token.getAccess_token()); | ||
82 | - parameterService.insertAccessToken(accessToken); | ||
83 | - } | ||
84 | - String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token.getAccess_token()); | 63 | + String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token); |
85 | JSONObject jsonObject= JSONObject.parseObject(js); | 64 | JSONObject jsonObject= JSONObject.parseObject(js); |
86 | wxSign1.setJsapi_ticket(jsonObject.getString("ticket")); | 65 | wxSign1.setJsapi_ticket(jsonObject.getString("ticket")); |
87 | String rawstring = "jsapi_ticket=" + wxSign1.getJsapi_ticket() + "&noncestr=" + wxSign1.getNoncestr() + "×tamp=" + wxSign1.getTimestamp() + "&url=" + url + ""; | 66 | String rawstring = "jsapi_ticket=" + wxSign1.getJsapi_ticket() + "&noncestr=" + wxSign1.getNoncestr() + "×tamp=" + wxSign1.getTimestamp() + "&url=" + url + ""; |
@@ -181,6 +160,11 @@ public class CommonController { | @@ -181,6 +160,11 @@ public class CommonController { | ||
181 | return list ; | 160 | return list ; |
182 | } | 161 | } |
183 | 162 | ||
163 | + | ||
164 | + public static String getToken() { | ||
165 | + return HttpClientUtils.httpGet2("http://121.40.30.78:9903/getToken"); | ||
166 | + } | ||
167 | + | ||
184 | public String Get(String url, String param) { | 168 | public String Get(String url, String param) { |
185 | String result = ""; | 169 | String result = ""; |
186 | BufferedReader in = null; | 170 | BufferedReader in = null; |
src/main/java/com/sincere/student/utils/HttpClientUtils.java
0 → 100644
@@ -0,0 +1,190 @@ | @@ -0,0 +1,190 @@ | ||
1 | +package com.sincere.student.utils; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONObject; | ||
4 | +import org.apache.http.HttpEntity; | ||
5 | +import org.apache.http.HttpStatus; | ||
6 | +import org.apache.http.client.config.RequestConfig; | ||
7 | +import org.apache.http.client.methods.CloseableHttpResponse; | ||
8 | +import org.apache.http.client.methods.HttpGet; | ||
9 | +import org.apache.http.client.methods.HttpPost; | ||
10 | +import org.apache.http.entity.StringEntity; | ||
11 | +import org.apache.http.impl.client.CloseableHttpClient; | ||
12 | +import org.apache.http.impl.client.HttpClients; | ||
13 | +import org.apache.http.util.EntityUtils; | ||
14 | + | ||
15 | +import java.io.IOException; | ||
16 | +import java.io.InputStream; | ||
17 | +import java.net.HttpURLConnection; | ||
18 | +import java.net.URL; | ||
19 | + | ||
20 | +/** | ||
21 | + * HttpClient4.3工具类 | ||
22 | + * @author chen | ||
23 | + * @version 1.0 | ||
24 | + * @date 2019/10/11 0011 10:17 | ||
25 | + */ | ||
26 | +public class HttpClientUtils { | ||
27 | + | ||
28 | + private static RequestConfig requestConfig = null; | ||
29 | + | ||
30 | + static { | ||
31 | + // 设置请求和传输超时时间 | ||
32 | + requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); | ||
33 | + } | ||
34 | + | ||
35 | + public static void main(String[] args) throws Exception { | ||
36 | + | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * post请求传输json参数 | ||
41 | + * @param url url地址 | ||
42 | + * @param jsonParam 参数 | ||
43 | + * @return | ||
44 | + */ | ||
45 | + public static JSONObject httpPostJson(String url, String jsonParam) { | ||
46 | + // post请求返回结果 | ||
47 | + CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
48 | + JSONObject jsonResult = null; | ||
49 | + HttpPost httpPost = new HttpPost(url); | ||
50 | + // 设置请求和传输超时时间 | ||
51 | + httpPost.setConfig(requestConfig); | ||
52 | + try { | ||
53 | + if (null != jsonParam) { | ||
54 | + // 解决中文乱码问题 | ||
55 | + StringEntity entity = new StringEntity(jsonParam, "utf-8"); | ||
56 | + entity.setContentEncoding("UTF-8"); | ||
57 | + entity.setContentType("application/json"); | ||
58 | + httpPost.setEntity(entity); | ||
59 | + } | ||
60 | + CloseableHttpResponse result = httpClient.execute(httpPost); | ||
61 | + // 请求发送成功,并得到响应 | ||
62 | + if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
63 | + String str = ""; | ||
64 | + // 读取服务器返回过来的json字符串数据 | ||
65 | + str = EntityUtils.toString(result.getEntity(), "utf-8"); | ||
66 | + // 把json字符串转换成json对象 | ||
67 | + jsonResult = JSONObject.parseObject(str); | ||
68 | + | ||
69 | + } | ||
70 | + } catch (IOException e) { | ||
71 | + e.printStackTrace(); | ||
72 | + } finally { | ||
73 | + httpPost.releaseConnection(); | ||
74 | + } | ||
75 | + return jsonResult; | ||
76 | + } | ||
77 | + | ||
78 | + /** | ||
79 | + * post请求传输String参数 例如:name=Jack&sex=1&type=2 | ||
80 | + * Content-type:application/x-www-form-urlencoded | ||
81 | + * @param url url地址 | ||
82 | + * @param strParam 参数 | ||
83 | + * @return | ||
84 | + */ | ||
85 | + public static JSONObject httpPost(String url, String strParam) { | ||
86 | + // post请求返回结果 | ||
87 | + CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
88 | + JSONObject jsonResult = null; | ||
89 | + HttpPost httpPost = new HttpPost(url); | ||
90 | + httpPost.setConfig(requestConfig); | ||
91 | + try { | ||
92 | + if (null != strParam) { | ||
93 | + // 解决中文乱码问题 | ||
94 | + StringEntity entity = new StringEntity(strParam, "utf-8"); | ||
95 | + entity.setContentEncoding("UTF-8"); | ||
96 | + entity.setContentType("application/x-www-form-urlencoded"); | ||
97 | + httpPost.setEntity(entity); | ||
98 | + } | ||
99 | + CloseableHttpResponse result = httpClient.execute(httpPost); | ||
100 | + // 请求发送成功,并得到响应 | ||
101 | + if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
102 | + String str = ""; | ||
103 | + // 读取服务器返回过来的json字符串数据 | ||
104 | + str = EntityUtils.toString(result.getEntity(), "utf-8"); | ||
105 | + // 把json字符串转换成json对象 | ||
106 | + jsonResult = JSONObject.parseObject(str); | ||
107 | + } | ||
108 | + } catch (IOException e) { | ||
109 | + | ||
110 | + } finally { | ||
111 | + httpPost.releaseConnection(); | ||
112 | + } | ||
113 | + return jsonResult; | ||
114 | + } | ||
115 | + | ||
116 | + /** | ||
117 | + * 发送get请求 | ||
118 | + * @param url 路径 | ||
119 | + * @return | ||
120 | + */ | ||
121 | + public static JSONObject httpGet(String url) { | ||
122 | + // get请求返回结果 | ||
123 | + JSONObject jsonResult = null; | ||
124 | + CloseableHttpClient client = HttpClients.createDefault(); | ||
125 | + // 发送get请求 | ||
126 | + HttpGet request = new HttpGet(url); | ||
127 | + request.setConfig(requestConfig); | ||
128 | + try { | ||
129 | + CloseableHttpResponse response = client.execute(request); | ||
130 | + // 请求发送成功,并得到响应 | ||
131 | + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
132 | + // 读取服务器返回过来的json字符串数据 | ||
133 | + HttpEntity entity = response.getEntity(); | ||
134 | + String strResult = EntityUtils.toString(entity, "utf-8"); | ||
135 | + // 把json字符串转换成json对象 | ||
136 | + jsonResult = JSONObject.parseObject(strResult); | ||
137 | + } | ||
138 | + } catch (IOException e) { | ||
139 | + | ||
140 | + } finally { | ||
141 | + request.releaseConnection(); | ||
142 | + } | ||
143 | + return jsonResult; | ||
144 | + } | ||
145 | + | ||
146 | + public static String httpGet2(String url) { | ||
147 | + // get请求返回结果 | ||
148 | + JSONObject jsonResult = null; | ||
149 | + String result = "" ; | ||
150 | + CloseableHttpClient client = HttpClients.createDefault(); | ||
151 | + // 发送get请求 | ||
152 | + HttpGet request = new HttpGet(url); | ||
153 | + request.setConfig(requestConfig); | ||
154 | + try { | ||
155 | + CloseableHttpResponse response = client.execute(request); | ||
156 | + // 请求发送成功,并得到响应 | ||
157 | + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { | ||
158 | + // 读取服务器返回过来的json字符串数据 | ||
159 | + HttpEntity entity = response.getEntity(); | ||
160 | + String strResult = EntityUtils.toString(entity, "utf-8"); | ||
161 | + // 把json字符串转换成json对象 | ||
162 | + //jsonResult = JSONObject.parseObject(strResult); | ||
163 | + result = strResult; | ||
164 | + } | ||
165 | + } catch (IOException e) { | ||
166 | + | ||
167 | + } finally { | ||
168 | + request.releaseConnection(); | ||
169 | + } | ||
170 | + return result; | ||
171 | + } | ||
172 | + | ||
173 | + | ||
174 | + public static InputStream GetFileInputStream(String fileUrl){ | ||
175 | + try{ | ||
176 | + URL url = new URL(fileUrl); | ||
177 | + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); | ||
178 | + //设置超时间为3秒 | ||
179 | + conn.setConnectTimeout(8*1000); | ||
180 | + //得到输入流 | ||
181 | + InputStream inputStream = conn.getInputStream(); | ||
182 | + | ||
183 | + return inputStream ; | ||
184 | + }catch (Exception e){ | ||
185 | + | ||
186 | + } | ||
187 | + return null ; | ||
188 | + } | ||
189 | + | ||
190 | +} |