ViewController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.zskk.control;
  2. import java.io.IOException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import com.alibaba.fastjson.JSON;
  10. import com.alibaba.fastjson.JSONArray;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.jfinal.core.Controller;
  13. import com.jfinal.kit.PropKit;
  14. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  15. import com.jfinal.plugin.activerecord.Db;
  16. import com.jfinal.plugin.activerecord.Record;
  17. import com.jfinal.plugin.activerecord.dialect.OracleDialect;
  18. import com.jfinal.plugin.druid.DruidPlugin;
  19. import com.zskk.tools.SSLSocketClient;
  20. import okhttp3.FormBody;
  21. import okhttp3.MediaType;
  22. import okhttp3.OkHttpClient;
  23. import okhttp3.Request;
  24. import okhttp3.RequestBody;
  25. import okhttp3.Response;
  26. public class ViewController extends Controller {
  27. private static final MediaType JSON_CODE = MediaType.get("application/json; charset=utf-8");
  28. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  29. /**
  30. * 在被连接数据库执行sql语句
  31. */
  32. public void executeSql() {
  33. try {
  34. List<Record> d = Db.use("connected").find(this.getPara("sqlstr"));
  35. this.renderJson(d);
  36. } catch (Exception e) {
  37. // TODO: handle exception
  38. this.renderText(e.toString());
  39. }
  40. }
  41. public void testget() {
  42. try {
  43. Map <String,String> map = new HashMap<String,String>();
  44. map.put("institution_id", "73090004");
  45. map.put("num","30");
  46. String content = postWithParameters("https://117.156.67.61:9600/butt/getExam", map);
  47. // JSONObject jsonObject = JSON.parseObject(content);
  48. // if (!jsonObject.getString("msg").equals("success")) {
  49. //// return null;
  50. // }
  51. // JSONArray jsonArray = JSON.parseArray(jsonObject.getString("data"));
  52. // return jsonArray;
  53. this.renderText(content);
  54. } catch (Exception e) {
  55. // TODO: handle exception
  56. this.renderText(e.toString());
  57. }
  58. }
  59. public static String postWithParameters(String url, Map<String, String> map) {
  60. OkHttpClient client = new OkHttpClient.Builder()
  61. .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
  62. .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
  63. .build();
  64. FormBody.Builder formbody = new FormBody.Builder();
  65. for (Map.Entry<String, String> entry : map.entrySet()) {
  66. formbody.add(entry.getKey(), entry.getValue());
  67. }
  68. RequestBody requestBody = formbody.build();
  69. Request request = new Request.Builder()
  70. .url(url)
  71. .post(requestBody)
  72. .build();
  73. try (Response response = client.newCall(request).execute()) {
  74. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  75. String content = response.body().string();
  76. return content;
  77. } catch (IOException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. return null;
  81. }
  82. }
  83. public static DruidPlugin createConnectedDruidPlugin() {
  84. return new DruidPlugin(PropKit.get("jdbcUrl_connected"), PropKit.get("user_connected"),PropKit.get("password_connected").trim());
  85. }
  86. public void testConn() {
  87. try {
  88. DruidPlugin druidPluginConnected = createConnectedDruidPlugin();
  89. druidPluginConnected.start();
  90. // 配置ActiveRecord插件
  91. ActiveRecordPlugin arpConnected = new ActiveRecordPlugin("connected", druidPluginConnected);
  92. arpConnected.setDialect(new OracleDialect());
  93. arpConnected.start();
  94. } catch (Exception e) {
  95. // TODO: handle exception
  96. this.renderText(e.toString());
  97. }}
  98. /**
  99. * post请求
  100. * @param url-请求地址
  101. * @param map-参数集合
  102. * @return
  103. */
  104. private static String doPost(String url, Map<String, String> map) {
  105. FormBody.Builder builder = new FormBody.Builder();
  106. for (String key : map.keySet()) {
  107. builder.add(key, map.get(key));
  108. }
  109. RequestBody formBody = builder.build();
  110. Request request = new Request.Builder().url(url).post(formBody).build();
  111. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  112. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  113. String content = response.body().string();
  114. return content;
  115. } catch (IOException e) {
  116. // TODO Auto-generated catch block
  117. e.printStackTrace();
  118. return null;
  119. }
  120. }
  121. /**
  122. * 日期字符串格式转换
  123. * @param dateStr
  124. * @return
  125. */
  126. private Date parseStringToDate(String dateStr) {
  127. if (dateStr == null) {
  128. return new Date();
  129. }
  130. SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  131. Date date = null;
  132. try {
  133. date = sdf.parse(dateStr);
  134. } catch (ParseException e) {
  135. // TODO Auto-generated catch block
  136. e.printStackTrace();
  137. }
  138. return date;
  139. }
  140. public static void main(String[] args) {
  141. Map<String,String> paramsMap=new HashMap<String,String>();
  142. paramsMap.put("institution_id", "44100001");
  143. String contentString = doPost("https://risserver3.pacsonline.cn/butt/getExam/butt/getExam", paramsMap);
  144. System.out.println(contentString);
  145. }
  146. }