HttpUtils.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. package cn.com.ctop.common.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import org.apache.fontbox.ttf.CmapSubtable;
  5. import org.apache.fontbox.ttf.GlyphData;
  6. import org.apache.fontbox.ttf.TTFParser;
  7. import org.apache.fontbox.ttf.TrueTypeFont;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.HttpStatus;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.CookieStore;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.config.CookieSpecs;
  16. import org.apache.http.client.config.RequestConfig;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. import org.apache.http.client.methods.CloseableHttpResponse;
  19. import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
  20. import org.apache.http.client.methods.HttpGet;
  21. import org.apache.http.client.methods.HttpPost;
  22. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  23. import org.apache.http.conn.ssl.TrustStrategy;
  24. import org.apache.http.cookie.Cookie;
  25. import org.apache.http.entity.ContentType;
  26. import org.apache.http.entity.StringEntity;
  27. import org.apache.http.impl.client.BasicCookieStore;
  28. import org.apache.http.impl.client.CloseableHttpClient;
  29. import org.apache.http.impl.client.HttpClientBuilder;
  30. import org.apache.http.impl.client.HttpClients;
  31. import org.apache.http.message.BasicNameValuePair;
  32. import org.apache.http.ssl.SSLContextBuilder;
  33. import org.apache.http.util.EntityUtils;
  34. import org.eclipse.jetty.util.UrlEncoded;
  35. import org.omg.CORBA.OBJ_ADAPTER;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import javax.net.ssl.SSLContext;
  39. import java.io.BufferedReader;
  40. import java.io.IOException;
  41. import java.io.InputStreamReader;
  42. import java.net.URI;
  43. import java.net.URL;
  44. import java.net.URLConnection;
  45. import java.net.URLEncoder;
  46. import java.nio.charset.Charset;
  47. import java.security.cert.CertificateException;
  48. import java.security.cert.X509Certificate;
  49. import java.util.*;
  50. public class HttpUtils {
  51. public static JSONObject bytedanceGetRequest(String accessToken, String url, Map<String, Object> params) {
  52. // 构造请求
  53. HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
  54. @Override
  55. public String getMethod() {
  56. return "GET";
  57. }
  58. };
  59. httpEntity.setHeader("Access-Token", accessToken);
  60. CloseableHttpResponse response = null;
  61. CloseableHttpClient client = null;
  62. try {
  63. client = HttpClientBuilder.create().build();
  64. httpEntity.setURI(URI.create(url));
  65. httpEntity.setEntity(new StringEntity(JSONObject.toJSONString(params), ContentType.APPLICATION_JSON));
  66. response = client.execute(httpEntity);
  67. if (response != null && response.getStatusLine().getStatusCode() == 200) {
  68. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  69. StringBuffer result = new StringBuffer();
  70. String line = "";
  71. while ((line = bufferedReader.readLine()) != null) {
  72. result.append(line);
  73. }
  74. bufferedReader.close();
  75. return JSONObject.parseObject(result.toString());
  76. }
  77. } catch (ClientProtocolException e) {
  78. e.printStackTrace();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. } finally {
  82. try {
  83. if (response != null) {
  84. response.close();
  85. }
  86. client.close();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91. return null;
  92. }
  93. private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
  94. public static CookieStore cookieStore = new BasicCookieStore();
  95. //.setProxy(new HttpHost("106.125.239.179", 4245))
  96. public static Map<String, Map<String, String>> fontsMap = new HashMap<String, Map<String, String>>();
  97. public static String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";
  98. public static CloseableHttpClient createSSLClientDefault() {
  99. try {
  100. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  101. //信任所有证书
  102. @Override
  103. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  104. return true;
  105. }
  106. }).build();
  107. RequestConfig globalConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).setCookieSpec(CookieSpecs.STANDARD).build();
  108. SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
  109. return HttpClients.custom().setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setConnectionReuseStrategy((response, context) -> false).setSSLSocketFactory(sslFactory).build();
  110. } catch (Exception e) {
  111. logger.error("处理Https证书异常", e);
  112. }
  113. return HttpClients.createDefault();
  114. }
  115. public static String httpPostParamRequest(String url, Map<String, Object> param, Map<String, String> headers) {
  116. HttpClient httpClient = createSSLClientDefault();
  117. String strReturn = "";
  118. try {
  119. HttpPost httppost = new HttpPost(url);
  120. httppost.setHeader("User-Agent", USER_AGENT);
  121. List<Cookie> list = cookieStore.getCookies();
  122. // for (Cookie ck : list) {
  123. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  124. // }
  125. // System.out.println("-------------------------------------");
  126. for (String key : headers.keySet()) {
  127. httppost.setHeader(key, headers.get(key));
  128. }
  129. List<NameValuePair> paramList = new ArrayList<NameValuePair>();
  130. for (String key : param.keySet()) {
  131. BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, String.valueOf(param.get(key)));
  132. paramList.add(basicNameValuePair);
  133. }
  134. // 第二步:我们发现Entity是一个接口,所以只能找实现类,发现实现类又需要一个集合,集合的泛型是NameValuePair类型
  135. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(paramList);
  136. // 第一步:通过setEntity 将我们的entity对象传递过去
  137. httppost.setEntity(formEntity);
  138. //httppost.setHeader("Content-Type", "application/json");
  139. // httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
  140. HttpEntity respentity;
  141. HttpResponse response = httpClient.execute(httppost);
  142. int statusCode = response.getStatusLine().getStatusCode();
  143. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  144. String newUrl = response.getFirstHeader("Location").getValue();
  145. return httpPostParamRequest(newUrl, param, headers);
  146. } else if (statusCode == HttpStatus.SC_OK) {
  147. respentity = response.getEntity();
  148. strReturn = EntityUtils.toString(respentity);
  149. return strReturn;
  150. }
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. logger.error(e.getMessage());
  154. }
  155. return strReturn;
  156. }
  157. public static String httpPostRequest(String url, Map<String, Object> param, Map<String, String> headers) {
  158. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  159. HttpClient httpClient = createSSLClientDefault();
  160. String strReturn = "";
  161. try {
  162. HttpPost httppost = new HttpPost(url);
  163. httppost.setHeader("User-Agent", USER_AGENT);
  164. httppost.addHeader("Content-Type", "application/json");
  165. List<Cookie> list = cookieStore.getCookies();
  166. // for (Cookie ck : list) {
  167. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  168. // }
  169. for (String key : headers.keySet()) {
  170. httppost.setHeader(key, headers.get(key));
  171. }
  172. //httppost.setHeader("Content-Type", "application/json");
  173. httppost.setEntity(new StringEntity(new Gson().toJson(param), Charset.forName("UTF-8")));
  174. HttpEntity respentity;
  175. HttpResponse response = httpClient.execute(httppost);
  176. int statusCode = response.getStatusLine().getStatusCode();
  177. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  178. String newUrl = response.getFirstHeader("Location").getValue();
  179. return httpPostRequest(newUrl, param, headers);
  180. } else if (statusCode == HttpStatus.SC_OK) {
  181. respentity = response.getEntity();
  182. strReturn = EntityUtils.toString(respentity);
  183. return strReturn;
  184. }
  185. } catch (Exception e) {
  186. e.printStackTrace();
  187. logger.error(e.getMessage());
  188. }
  189. return strReturn;
  190. }
  191. public static String httpPostRequestTest(String url, String body, Map<String, String> headers) {
  192. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  193. HttpClient httpClient = createSSLClientDefault();
  194. String strReturn = "";
  195. try {
  196. HttpPost httppost = new HttpPost(url);
  197. httppost.setHeader("User-Agent", USER_AGENT);
  198. httppost.addHeader("Content-Type", "application/json");
  199. List<Cookie> list = cookieStore.getCookies();
  200. for (Cookie ck : list) {
  201. System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  202. }
  203. for (String key : headers.keySet()) {
  204. httppost.setHeader(key, headers.get(key));
  205. }
  206. //httppost.setHeader("Content-Type", "application/json");
  207. httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
  208. HttpEntity respentity;
  209. HttpResponse response = httpClient.execute(httppost);
  210. int statusCode = response.getStatusLine().getStatusCode();
  211. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  212. String newUrl = response.getFirstHeader("Location").getValue();
  213. return httpPostRequestTest(newUrl, body, headers);
  214. } else if (statusCode == HttpStatus.SC_OK) {
  215. respentity = response.getEntity();
  216. strReturn = EntityUtils.toString(respentity);
  217. return strReturn;
  218. }
  219. } catch (Exception e) {
  220. e.printStackTrace();
  221. logger.error(e.getMessage());
  222. }
  223. return strReturn;
  224. }
  225. public static String kuaiShouhttpPostRequest(String url, String body, Map<String, String> headers) {
  226. System.err.println(body);
  227. HttpClient httpClient = createSSLClientDefault();
  228. String strReturn = "";
  229. try {
  230. HttpPost httppost = new HttpPost(url);
  231. if (!Check.isNullMap(headers)) {
  232. for (String key : headers.keySet()) {
  233. httppost.setHeader(key, headers.get(key));
  234. }
  235. }
  236. httppost.setHeader("User-Agent", USER_AGENT);
  237. if (!Check.isNull(body)) {
  238. httppost.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
  239. }
  240. HttpEntity respentity;
  241. System.err.println(httppost);
  242. HttpResponse response = httpClient.execute(httppost);
  243. int statusCode = response.getStatusLine().getStatusCode();
  244. if (statusCode == HttpStatus.SC_OK) {
  245. respentity = response.getEntity();
  246. strReturn = EntityUtils.toString(respentity);
  247. return strReturn;
  248. }
  249. } catch (Exception e) {
  250. e.printStackTrace();
  251. logger.error(e.getMessage());
  252. }
  253. return strReturn;
  254. }
  255. public static String httpPostRequest(String url, JSONObject params, Map<String, String> headers) {
  256. // HttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
  257. HttpClient httpClient = createSSLClientDefault();
  258. String strReturn = "";
  259. try {
  260. HttpPost httppost = new HttpPost(url);
  261. httppost.setHeader("User-Agent", USER_AGENT);
  262. httppost.addHeader("Content-Type", "application/json");
  263. List<Cookie> list = cookieStore.getCookies();
  264. // for (Cookie ck : list) {
  265. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  266. // }
  267. for (String key : headers.keySet()) {
  268. httppost.setHeader(key, headers.get(key));
  269. }
  270. //httppost.setHeader("Content-Type", "application/json");
  271. httppost.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
  272. HttpEntity respentity;
  273. HttpResponse response = httpClient.execute(httppost);
  274. int statusCode = response.getStatusLine().getStatusCode();
  275. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  276. String newUrl = response.getFirstHeader("Location").getValue();
  277. return httpPostRequest(newUrl, params, headers);
  278. } else if (statusCode == HttpStatus.SC_OK) {
  279. respentity = response.getEntity();
  280. strReturn = EntityUtils.toString(respentity);
  281. return strReturn;
  282. }
  283. } catch (Exception e) {
  284. e.printStackTrace();
  285. logger.error(e.getMessage());
  286. }
  287. return strReturn;
  288. }
  289. public static String httpGetRequest(String url) {
  290. HttpClient httpClient = createSSLClientDefault();
  291. HttpResponse response = null;
  292. HttpGet httpGet = new HttpGet(url);
  293. // httpPost.setHeader("Content-Type", "application/json");
  294. String result = null;
  295. try {
  296. httpGet.setHeader("User-Agent", USER_AGENT);
  297. List<Cookie> list2 = cookieStore.getCookies();
  298. // for (Cookie ck : list2) {
  299. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  300. // }
  301. response = httpClient.execute(httpGet);
  302. List<Cookie> list = cookieStore.getCookies();
  303. // for (Cookie ck : list) {
  304. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  305. // }
  306. int statusCode = response.getStatusLine().getStatusCode();
  307. if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
  308. String newUrl = response.getFirstHeader("Location").getValue();
  309. return httpGetRequest(newUrl);
  310. } else if (statusCode == HttpStatus.SC_OK) {
  311. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  312. String line = null;
  313. StringBuilder builder = new StringBuilder();
  314. while ((line = reader.readLine()) != null) {
  315. builder.append(line);
  316. }
  317. result = builder.toString();
  318. }
  319. } catch (Exception e) {
  320. e.printStackTrace();
  321. }
  322. return result;
  323. }
  324. public static String httpRequest(String url, String strParams) throws Exception {
  325. // HttpClient httpclient = HttpClientBuilder.create().build();
  326. HttpClient httpClient = createSSLClientDefault();
  327. String strReturn = "";
  328. try {
  329. HttpPost httppost = new HttpPost(url);
  330. httppost.addHeader("Content-Type", "application/json");
  331. httppost.setEntity(new StringEntity(strParams, Charset.forName("UTF-8")));
  332. HttpEntity respentity;
  333. HttpResponse response = httpClient.execute(httppost);
  334. int code = response.getStatusLine().getStatusCode();
  335. respentity = response.getEntity();
  336. strReturn = EntityUtils.toString(respentity);
  337. } catch (Exception e) {
  338. e.printStackTrace();
  339. logger.error(e.getMessage());
  340. }
  341. return strReturn;
  342. }
  343. public static String httpRequest(String url, CookieStore cookies, Map<String, String> parameterMap) throws Exception {
  344. // HttpClient httpclient = getHttpclient();
  345. HttpClient httpClient = createSSLClientDefault();
  346. String strReturn = "";
  347. try {
  348. HttpPost httppost = new HttpPost(url);
  349. httppost.setHeader("Content-Type", "application/json");
  350. UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(
  351. getParam(parameterMap), "UTF-8");
  352. httppost.setEntity(postEntity);
  353. HttpEntity respentity;
  354. HttpResponse response = httpClient.execute(httppost);
  355. respentity = response.getEntity();
  356. strReturn = EntityUtils.toString(respentity);
  357. } catch (Exception e) {
  358. e.printStackTrace();
  359. logger.error(e.getMessage());
  360. }
  361. return strReturn;
  362. }
  363. public static List<NameValuePair> getParam(Map parameterMap) {
  364. List<NameValuePair> param = new ArrayList<NameValuePair>();
  365. Iterator it = parameterMap.entrySet().iterator();
  366. while (it.hasNext()) {
  367. Map.Entry parmEntry = (Map.Entry) it.next();
  368. param.add(new BasicNameValuePair((String) parmEntry.getKey(),
  369. (String) parmEntry.getValue()));
  370. }
  371. return param;
  372. }
  373. public static HttpClient getHttpclient() {
  374. RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(180000).setConnectTimeout(30000)
  375. .setConnectionRequestTimeout(30000).setStaleConnectionCheckEnabled(true).build();
  376. CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
  377. return httpclient;
  378. }
  379. public static String callingGraph(String url, String json) {
  380. HttpClient httpClient = createSSLClientDefault();
  381. HttpResponse response = null;
  382. HttpPost httpPost = new HttpPost(url);
  383. List<Cookie> list = cookieStore.getCookies();
  384. // cookieStore.clear();
  385. // for (Cookie ck : list) {
  386. //
  387. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  388. // }
  389. // System.out.println("-------------------------------------");
  390. httpPost.setHeader("Content-Type", "application/json");
  391. String result = null;
  392. try {
  393. StringEntity entity = new StringEntity(json, "utf-8");
  394. httpPost.setHeader("User-Agent", USER_AGENT);
  395. httpPost.setEntity(entity);
  396. response = httpClient.execute(httpPost);
  397. BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  398. String line = null;
  399. StringBuilder builder = new StringBuilder();
  400. while ((line = reader.readLine()) != null) {
  401. builder.append(line);
  402. }
  403. // for (Cookie ck : list) {
  404. //
  405. // System.out.println(ck.getName() + "=" + ck.getValue() + "," + ck.getDomain());
  406. // }
  407. result = builder.toString();
  408. } catch (Exception e) {
  409. e.printStackTrace();
  410. }
  411. return result;
  412. }
  413. public static Map<String, Integer> kuaishouNumberMap;
  414. public static Integer getKuaishouNumber(String key) {
  415. if (kuaishouNumberMap == null) {
  416. kuaishouNumberMap = new HashMap<>();
  417. // kuaishouNumberMap.put("32.0#-6.0#526.0#729.0",0);//735
  418. // kuaishouNumberMap.put("98.0#13.0#363.0#726.0",1);//713
  419. // kuaishouNumberMap.put("32.0#13.0#527.0#732.0",2);//719
  420. // kuaishouNumberMap.put("25.0#-6.0#525.0#730.0",3);//736
  421. // kuaishouNumberMap.put("26.0#13.0#536.0#731.0",4);//718
  422. // kuaishouNumberMap.put("33.0#-5.0#526.0#717.0",5);//722
  423. // kuaishouNumberMap.put("39.0#-5.0#530.0#732.0",6);//737
  424. // kuaishouNumberMap.put("38.0#13.0#536.0#717.0",7);//704
  425. // kuaishouNumberMap.put("33.0#-7.0#525.0#731.0",8);//738
  426. // kuaishouNumberMap.put("37.0#-7.0#521.0#730.0",9);//737
  427. kuaishouNumberMap.put("494.0#735.0", 0);
  428. kuaishouNumberMap.put("265.0#713.0", 1);
  429. kuaishouNumberMap.put("495.0#719.0", 2);
  430. kuaishouNumberMap.put("500.0#736.0", 3);
  431. kuaishouNumberMap.put("510.0#718.0", 4);
  432. kuaishouNumberMap.put("493.0#722.0", 5);
  433. kuaishouNumberMap.put("491.0#737.0", 6);
  434. kuaishouNumberMap.put("498.0#704.0", 7);
  435. kuaishouNumberMap.put("492.0#738.0", 8);
  436. kuaishouNumberMap.put("484.0#737.0", 9);
  437. }
  438. return kuaishouNumberMap.get(key);
  439. }
  440. public static void kuaishouTtf(String ttfName) {
  441. try {
  442. URL url = new URL("https://static.yximgs.com/udata/pkg/kuaishou-front-end-live/" + ttfName); // 创建URL
  443. URLConnection urlconn = url.openConnection();
  444. TTFParser ttfParser = new TTFParser();
  445. TrueTypeFont ttf = ttfParser.parse(urlconn.getInputStream());
  446. // System.out.println(new Gson().toJson(ttf.getGlyph().getGlyphs()));
  447. GlyphData[] datas = ttf.getGlyph().getGlyphs();
  448. CmapSubtable[] tables = ttf.getCmap().getCmaps();
  449. Gson gson = new Gson();
  450. // System.out.println(gson.toJson(tables));
  451. CmapSubtable table = tables[0];
  452. Map<String, Integer> numberMap = new HashMap<String, Integer>();
  453. Map<String, String> fontMap = new HashMap<String, String>();
  454. for (int i = 0; i <= 13; i++) {
  455. GlyphData data = datas[i];
  456. if (data != null) {
  457. // System.out.println("getBoundingBox" + data.getBoundingBox());
  458. float fx = data.getBoundingBox().getLowerLeftX();
  459. float fy = data.getBoundingBox().getLowerLeftY();
  460. float rx = data.getBoundingBox().getUpperRightX();
  461. float ry = data.getBoundingBox().getUpperRightY();
  462. Integer num = getKuaishouNumber(String.valueOf(rx - fx) + "#" + String.valueOf(ry - fy));
  463. // if (num != null) {
  464. // System.out.println(num);
  465. // }
  466. fontMap.put(String.valueOf((char) table.getCharCodes(i).get(0).intValue()), String.valueOf(num));
  467. }
  468. }
  469. // System.out.println(new Gson().toJson(fontMap));
  470. fontsMap.put(ttfName, fontMap);
  471. for (GlyphData data : datas) {
  472. if (data != null) {
  473. // System.out.println("getBoundingBox" + data.getBoundingBox());
  474. float fx = data.getBoundingBox().getLowerLeftX();
  475. float fy = data.getBoundingBox().getLowerLeftY();
  476. float rx = data.getBoundingBox().getUpperRightX();
  477. float ry = data.getBoundingBox().getUpperRightY();
  478. Integer num = getKuaishouNumber(String.valueOf(fx) + "#" + String.valueOf(fy) + "#" + String.valueOf(rx) + "#" + String.valueOf(ry));
  479. // System.out.println(num);
  480. }
  481. }
  482. // System.out.println(new Gson().toJson(numberMap));
  483. } catch (Exception e) {
  484. e.printStackTrace();
  485. }
  486. }
  487. public static String mapParamsSortToStringBySeperator(TreeMap<String, Object> treeMap, String seperator) {
  488. String result = "";
  489. if (null != treeMap && !treeMap.isEmpty()) {
  490. for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
  491. String key = entry.getKey();
  492. String value = UrlEncoded.encodeString((String) entry.getValue());
  493. result += (key + "=" + value + seperator);
  494. }
  495. if (result.length() > 0) {
  496. result = result.substring(0, result.length() - seperator.length());
  497. }
  498. }
  499. return result;
  500. }
  501. public static String httpGetRequest(String url, Map<String, String> headers, TreeMap<String, Object> params) {
  502. HttpClient httpClient = createSSLClientDefault();
  503. String strReturn = "";
  504. try {
  505. String uri = url + "?" + mapParamsSortToStringBySeperator(params, "&");
  506. HttpGet httpGet = new HttpGet(uri);
  507. if (headers != null) {
  508. Iterator<String> keyIter = headers.keySet().iterator();
  509. while (keyIter.hasNext()) {
  510. String curKey = keyIter.next();
  511. if (curKey != null && headers.get(curKey) != null) {
  512. httpGet.addHeader(curKey, headers.get(curKey));
  513. }
  514. }
  515. }
  516. HttpEntity respentity;
  517. HttpResponse response = httpClient.execute(httpGet);
  518. respentity = response.getEntity();
  519. strReturn = EntityUtils.toString(respentity);
  520. } catch (Exception e) {
  521. e.printStackTrace();
  522. logger.error(e.getMessage());
  523. }
  524. return strReturn;
  525. }
  526. public static String httpGetRequest(String url, Map<String, String> headers, JSONObject params) {
  527. HttpClient httpClient = createSSLClientDefault();
  528. String strReturn = "";
  529. try {
  530. HttpGetWithBodyEntity httpGet = new HttpGetWithBodyEntity(url);
  531. if (headers != null) {
  532. Iterator<String> keyIter = headers.keySet().iterator();
  533. while (keyIter.hasNext()) {
  534. String curKey = keyIter.next();
  535. if (curKey != null && headers.get(curKey) != null) {
  536. httpGet.addHeader(curKey, headers.get(curKey));
  537. }
  538. }
  539. }
  540. httpGet.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
  541. HttpEntity respentity;
  542. HttpResponse response = httpClient.execute(httpGet);
  543. respentity = response.getEntity();
  544. strReturn = EntityUtils.toString(respentity);
  545. } catch (Exception e) {
  546. e.printStackTrace();
  547. logger.error(e.getMessage());
  548. }
  549. return strReturn;
  550. }
  551. public static String httpGet(String url, Map<String, Object> paramsMap, Map<String, String> headers) {
  552. String result = null;
  553. try {
  554. // CloseableHttpClient httpClient = HttpClients.createDefault();
  555. //创建参数列表
  556. HttpClient httpclient = getHttpclient();
  557. StringBuilder postBody = null;
  558. if (!Check.isNull(paramsMap)) {
  559. postBody = new StringBuilder();
  560. for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
  561. if (entry.getValue() == null) {
  562. continue;
  563. }
  564. postBody.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue().toString(),
  565. "utf-8")).append("&");
  566. }
  567. if (!paramsMap.isEmpty()) {
  568. postBody.deleteCharAt(postBody.length() - 1);
  569. }
  570. }
  571. String urlStr;
  572. if (!Check.isNull(postBody)) {
  573. urlStr = url + "?" + postBody;
  574. } else {
  575. urlStr = url;
  576. }
  577. HttpGet httpget = new HttpGet(urlStr);
  578. httpget.setHeader("User-Agent", USER_AGENT);
  579. if (!Check.isNullMap(headers)) {
  580. for (String key : headers.keySet()) {
  581. httpget.setHeader(key, headers.get(key));
  582. }
  583. }
  584. HttpEntity respEntity;
  585. System.err.println(httpget);
  586. HttpResponse response = httpclient.execute(httpget);
  587. respEntity = response.getEntity();
  588. result = EntityUtils.toString(respEntity);
  589. } catch (Exception e) {
  590. logger.error("http get请求异常");
  591. e.printStackTrace();
  592. }
  593. return result;
  594. }
  595. }